计算程序
- scanner关闭后新建出现异常NoSuchElementException
import java.util.Scanner;
public class calculatorTest {
public static void main(String[] args) {
while(true) {//保证循环
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数");
double a1 = scanner.nextDouble();//第二次运行到此处时报错,处理办法是删掉scanner.close()
System.out.println("请输入第二个数");
double a2 = scanner.nextDouble();
System.out.println("请输入运算符");
String str = scanner.next();
switch (str) {
case "+":
System.out.println(add(a1, a2));
break;
case "-":
System.out.println(subtraction(a1, a2));
break;
case "*":
System.out.println(multiplication(a1, a2));
break;
case "/":
System.out.println(division(a1, a2));
break;
default:
System.out.println("符号输入错误,请重新输入");
}
scanner.close();
}
}
public static double add(double x,double y){//定义加法
return x+y;
}
public static double subtraction(double x,double y){//定义减法
return x-y;
}
public static double multiplication(double x,double y){//定义乘法
return x*y;
}
public static double division(double x,double y){//定义除法
return x/y;
}
}
标签:scanner,double,System,暂未,问题,println,解决,public,out
From: https://www.cnblogs.com/0720hzq/p/17331637.html