使用 switch case 的 Java 计算器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26692689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java calculator using switch case
提问by Katty Hymanson
My requirement - calc
method should take two numbers from main
, and calc
will perform all the operations. Everything goes fine until a problem occurs in switch
command. I get the error "choice cannot be resolved to a variable".
我的要求 -calc
方法应该从 中获取两个数字main
,calc
并将执行所有操作。一切顺利,直到switch
命令出现问题。我收到错误“无法将选择解析为变量”。
import java.util.Scanner;
public class Learn {
public static void main(String args[]) {
int firstnumber, secondnumber, choice;
System.out.println("1- Add");
System.out.println("2- Sub");
System.out.println("3- Div");
System.out.println("4- Mul");
System.out.print("Enter your choice -");
Scanner var = new Scanner(System.in);
choice = var.nextInt();
System.out.print("Enter first number -");
firstnumber = var.nextInt();
System.out.print("Enter second number -");
secondnumber = var.nextInt();
calc(firstnumber, secondnumber);
}
public static void calc(int x, int y) {
int c;
switch (choice) {
case 1:
c = x + y;
System.out.print("Output-" + c);
break;
case 2:
c = x - y;
System.out.print("Output-" + c);
break;
case 3:
c = x / y;
System.out.print("Output-" + c);
break;
case 4:
c = x * y;
System.out.print("Output-" + c);
break;
}
}
}
What am I missing, and how can I fix this?
我错过了什么,我该如何解决这个问题?
回答by dasblinkenlight
Since choice
is a local in a different function, you need to pass it as a parameter:
由于choice
是不同函数中的局部变量,因此您需要将其作为参数传递:
public static void calc(int x, int y, int choice) {
...
}
...
calc (firstnumber,secondnumber, choice);
Note that your calc
method is not optimal: all four case
s have the same line in them:
请注意,您的calc
方法不是最佳方法:所有四个case
s 中都有相同的行:
System.out.print("Output-" + c);
You could move this line to after the switch
, and add a default case to throw an exception when the choice is invalid.
您可以将此行移至 之后switch
,并添加默认情况以在选择无效时引发异常。
回答by J0e3gan
You will need to pass choice
as an argument to calc
if you wish to use its value in calc
- something like this tweak to your code:
如果您希望使用它的值,则需要将其choice
作为参数传递给- 对您的代码进行如下调整:calc
calc
import java.util.Scanner;
public class Learn {
public static void main(String args[]) {
int firstnumber, secondnumber, choice;
System.out.println("1- Add");
System.out.println("2- Sub");
System.out.println("3- Div");
System.out.println("4- Mul");
System.out.print("Enter your choice -");
Scanner var = new Scanner(System.in);
choice = var.nextInt();
System.out.print("Enter first number -");
firstnumber = var.nextInt();
System.out.print("Enter second number -");
secondnumber = var.nextInt();
calc(choice, firstnumber, secondnumber); // 3rd arg added for choice
}
public static void calc(int choice, int x, int y) { // 3rd param added for choice
int c;
switch (choice) {
case 1:
c = x + y;
System.out.print("Output-" + c);
break;
case 2:
c = x - y;
System.out.print("Output-" + c);
break;
case 3:
c = x / y;
System.out.print("Output-" + c);
break;
case 4:
c = x * y;
System.out.print("Output-" + c);
break;
}
}
}