Calculator_Switch Java基础
Calculator_Switch JavaFundamental
您好,我试过计算器的语法,使用字符作为操作数
但结果总是错误。
我正在使用日食(Luna)。上面没有错误或警告消息。
Syntax
Scanner in = new Scanner(System.in);
int answer = 0;
System.out.println("Enter a number:");
int num1 = in.nextInt();
System.out.println("Enter another number:");
int num2 = in.nextInt();
System.out.println("Enter the operand:");
char input =(char) in.nextInt();
switch (input){
case'*':
answer = num1 * num2;
break;
case '/':
answer = num1/num2;
break;
case '%':
answer = num1%num2;
break;
case '+':
answer = num1 +num2;
break;
case '-':
answer = num1-num2;
break;
default:
System.out.println("Invalid Command");
}
System.out.println("The result is: " + answer);
Debugging
输入数字,另一个数字和一个操作数:
和错误:
输入不匹配异常
只需更改此行:char input =(char) in.nextInt();
为此 char input = in.next().charAt(0)
...您就可以开始了!
您好,我试过计算器的语法,使用字符作为操作数 但结果总是错误。 我正在使用日食(Luna)。上面没有错误或警告消息。
Syntax
Scanner in = new Scanner(System.in);
int answer = 0;
System.out.println("Enter a number:");
int num1 = in.nextInt();
System.out.println("Enter another number:");
int num2 = in.nextInt();
System.out.println("Enter the operand:");
char input =(char) in.nextInt();
switch (input){
case'*':
answer = num1 * num2;
break;
case '/':
answer = num1/num2;
break;
case '%':
answer = num1%num2;
break;
case '+':
answer = num1 +num2;
break;
case '-':
answer = num1-num2;
break;
default:
System.out.println("Invalid Command");
}
System.out.println("The result is: " + answer);
Debugging
输入数字,另一个数字和一个操作数:
和错误:
输入不匹配异常
只需更改此行:char input =(char) in.nextInt();
为此 char input = in.next().charAt(0)
...您就可以开始了!