练习题:计算器(只输入两个数运算)

CV:

import java.util.Scanner;

public class Test01 {
public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    while (true){
        System.out.println("开始计算请按Y,关闭请按N");
        String a = scanner.next();

        if (a.equals("Y")){
            System.out.println("请输入第一个数:");
            float num1 = scanner.nextFloat();

            System.out.println("请输入运算符:");
            String operator = scanner.next();

            System.out.println("请输入第二个数:");
            float num2 = scanner.nextFloat();

            switch (operator){
                case "+":
                    add(num1,num2);
                    break;
                case "-":
                    minus(num1,num2);
                    break;
                case "*":
                    multiply(num1,num2);
                    break;
                case "/":
                    divide(num1,num2);
                    break;
            }
        }else {
            System.out.println("关闭");
            break;
        }
    }
    scanner.close();
}
public static void add(float a,float b){
    System.out.println("a+b="+(a+b));
}
public static void minus(float a,float b){
    System.out.println("a-b="+(a-b));
}
public static void multiply(float a,float b){
    System.out.println("a*b="+(a*b));
}
public static void divide(float a,float b){
    System.out.println("a/b="+(a/b));
}

}

posted on 2022-08-03 21:30  凶残的小笼包  阅读(57)  评论(0编辑  收藏  举报

导航