码迷,mamicode.com
首页 > 编程语言 > 详细

Java实现一个简易计算器

时间:2021-04-09 12:50:24      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:cto   while   item   public   string   system   next   ddd   static   

做一个多功能计算器 ====== 欢迎使用计算器系统 ========

  1. int + int

  2. double + double 、

  3. 计算 n 的阶乘

  4. 计算 a的 n次方、

  5. 退出系统

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        //计算器界面
        calInter();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入功能选项:");
        int i = sc.nextInt();
        int j = 1;
        while(true){
            switch (i){
                case 1:
                    System.out.println("结果为:"+addInt());
                    break;
                case 2:
                    System.out.println("结果为:"+addDouble());
                    break;
                case 3:
                    System.out.println("结果为:"+factor());
                    break;
                case 4:
                    System.out.println("结果为:"+power());
                    break;
                case 5:
                    exit();
                    j = 0;
                    break;
                default:
                    System.out.println("输入功能选项有误,您只能选择1~5之间的整数,请重新输入!");
            }
            if(j == 0){
                break;
            }
            System.out.println("请输入功能选项:");
            i = sc.nextInt();
        }
    }
    //计算器界面
    public static void calInter(){
        System.out.println("====== 欢迎使用计算器系统 ========");
        System.out.println("1. int + int");
        System.out.println("2. double + double ");
        System.out.println("3. 计算 n 的阶乘");
        System.out.println("4. 计算 a的 n次方");
        System.out.println("5. 退出系统");
    }
    //Int加法
    public static int addInt(){
        Scanner sc = new Scanner(System.in);
        System.out.println("第一个int数为:");
        int a = sc.nextInt();
        System.out.println("第二个int数为:");
        int b = sc.nextInt();
        return a + b;
    }
    //double加法
    public static double addDouble(){
        Scanner sc = new Scanner(System.in);
        System.out.println("第一个double数为:");
        double a = sc.nextDouble();
        System.out.println("第二个数double为:");
        double b = sc.nextDouble();
        return a + b;
    }
    //n的阶乘
    public static int factor(){
        Scanner sc = new Scanner(System.in);
        System.out.println("阶乘n为:");
        int n = sc.nextInt();
        int fact = 1;
        for(int i = 1; i <= n ;i++){
            fact *= i;
        }
        return fact;
    }
    //a的n次方
    public static double power(){
        Scanner sc = new Scanner(System.in);
        System.out.println("底数a为:");
        int a = sc.nextInt();
        System.out.println("幂数n为:");
        int n = sc.nextInt();
        return Math.pow(a,n);
    }
    //退出系统
    public static void exit(){
        System.out.println("退出系统");
    }


}

 

Java实现一个简易计算器

标签:cto   while   item   public   string   system   next   ddd   static   

原文地址:https://www.cnblogs.com/lsm-boke/p/14631619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!