public class calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = 1;
while (t ==1) { //用来循环输入
System.out.println("输入第一个数:");
double n = scanner.nextDouble();
System.out.println("输入符号");
String v = scanner.next();
System.out.println("输入第二个数");
double m = scanner.nextDouble();
switch (v) {
case "+":
double c1 = add(n, m);
System.out.println(c1);
break;
case "-":
double c2 = sub(n, m);
System.out.println(c2);
break;
case "*":
double c3 = mul(n, m);
System.out.println(c3);
break;
case "/":
if (m == 0) {
System.out.println("除数不能为0,请重新输入!");
} else {
double c4 = div(n, m);
System.out.println(c4);
}
break;
}
System.out.println("是否进行再次运算?输入YES或者NO");
String str=scanner.next();
if(str.equals("YES")){//①对于字符串类型来说,"=="运算符用于比较两个变量本身的值,即两个对象在内存中的首地址
t=1; //equals()方法则是比较两个字符串中所包含的内容是否相同
}else{ //②对于非字符串类型的变量来说,"=="运算符和equals方法都用来比较两个对象在内存中的首地址
t=0;
scanner.close();
}
}
}
public static double add(double a,double b){
double c=a+b;
return c;
}
public static double sub(double a,double b){
double c=a-b;
return c;
}
public static double mul(double a,double b){
double c=a*b;
return c;
}
public static double div(double a,double b){
double c=a+b;
return c;
}
}
标签:java,scanner,double,System,简易,计算器,println,public,out
From: https://www.cnblogs.com/zwy1823/p/16793286.html