Java入门:简易计算器
package com.li.base;
import java.util.Scanner;
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(“请输入第二个数”);
double b = scanner.nextDouble();
System.out.println(“请选择运算“+”、“-”、“×”或“÷””);
String c = scanner.next();
switch ©{
case “+”:
System.out.println(add(a,b));
break;
case “-”:
System.out.println(sub(a,b));
break;
case “×”:
System.out.println(multiply(a,b));
break;
case “÷”:
System.out.println(divide(a,b));
break;
}
}
public static double add(double a, double b){
double sum = a+b;
return sum;
}
public static double sub(double a,double b){
double minus = a - b;
return minus;
}
public static double multiply(double a,double b){
double mul = a * b;
return mul;
}
public static double divide(double a,double b){
double div = a/b;
return div;
}
}
Scanner类中的next和nextLine方法没弄明白,导致字符串c没传入控制台。