小白简易计算器
第一次尝试写代码
public class Calculator {
public static void main(String[] args) {
//创建扫描对象
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数据:");
double a = scanner.nextDouble();
System.out.println("请输入运算符:");
String operator = scanner.next();
System.out.println("请输入第二个数据:");
double b = scanner.nextDouble();
double result = 0;//储存结果
//根据输入的运算符调用不同方法
if ("+".equals(operator)) {// TODO equals 判断字符串是否相等
result = add(a , b);
}else if ("-".equals(operator)){
result = sub(a , b);
}else if ("*".equals(operator)){
result = multiply(a , b);
}else if ("/".equals(operator)){
result = div(a , b);
}else {
System.out.println("输入错误");
}
System.out.println("结果为:"+ result);
scanner.close();//关闭
}
//+-*/
public static double add(double a, double b) {
return a + b;
}
public static double sub(double a, double b) {
return a - b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static double div(double a, double b) {
return a / b;
}
}
标签:Java,scanner,double,System,简易,static,result,计算器,public From: https://www.cnblogs.com/GGcrane/p/16586504.html