程序猿胖虎
2021-10-27 10:52
采纳率: 0%
浏览 18

java加减乘除计算器可以正常运行,有些相关内容不能提示,怎么解决?

import java.util.Scanner;
class Operator{
    double num1 , num2;
    String s;
    public Operator(){  
        
  }
    public Operator(double num1 , String s , double num2) {
        num1 = num1;
        num2 = num2;
        s = s;
    }
    double getValue(double num1 , String b , double num2) {
        switch(b) {
        case "+" :return num1+num2;
        case "-" :return num1-num2;
        case "*" :return num1*num2;
        case "/" :
            if(num2 == 0) {
                return -1;
            }else {
                return num1/num2;
            }
        default:
            return 0;
        }
    }
}
public class Demo2 {
    public static void main(String[] args) {
        System.out.println("请输入你想要计算的表达式(用空格分开)");
        Scanner sc = new Scanner(System.in);
        double a = sc.nextDouble();
        String s = sc.next();
        double b = sc.nextDouble();
        Operator x = new Operator();
        if(x.getValue(a,s,b) == 0) {
            System.out.print("请输入正确的格式!");
        }else if(x.getValue(a,s,b) == -1) {
            System.out.print("分母不能为0,请正确输入!");
        }else {
            System.out.print(x.getValue(a,s,b));
        }
    }
}

1条回答 默认 最新

相关推荐 更多相似问题