例如,如果我写这样的代码:
double result = 0;
switch (operation) {
case "divide":
double result = (double) first / second;
string = Double.toString(result);
break;
case "multiply":
double result = (double) first * second;
string = Double.toString(result);
break;
case "substract":
double result = (double) first - second;
string = Double.toString(result);
break;
case "add":
double result = (double) first + second;
string = Double.toString(result);
break;
}
它返回错误:变量'结果'已经在范围内定义。
我知道如何解决这个问题。我可以在每种情况下创建一个不同的变量,但这会因为必须为每个变量编写代码而延长代码。
如何按照给定的代码编写这项工作?
答案 0 :(得分:4)
这是因为result
已在switch
语句的顶部定义,并且您在声明中再次声明它。
尝试这样的事情:
switch (operation) {
case "divide":
double result = (double) first / second;
string = Double.toString(result);
break;
case "multiply":
double result = (double) first * second;
string = Double.toString(result);
break;
case "substract":
double result = (double) first - second;
string = Double.toString(result);
break;
case "add":
double result = (double) first + second;
string = Double.toString(result);
break;
}
换句话说,除了您的初始声明之外,您之前拥有的是什么。
或者,您可以声明double
并在每种情况下使用它:
double result = 0;
switch (operation) {
case "divide":
result = (double) first / second;
string = Double.toString(result);
break;
...
正如 Yassin Hajaj 在评论中指出的那样,如果你在{{1}结束时使用string = Double.toString(result);
行一次会好得多语句,而不是多次重复。
这看起来像这样:
switch
答案 1 :(得分:2)
您不能在使用全局结果变量的代码中的一个范围内声明相同的变量名称,也可以在大小写范围内使用相同的变量。
double result = 0;
switch (operation) {
case "divide":
result = (double) first / second;
string = Double.toString(result);
break;
case "multiply":
result = (double) first * second;
string = Double.toString(result);
break;
case "substract":
result = (double) first - second;
string = Double.toString(result);
break;
case "add":
result = (double) first + second;
string = Double.toString(result);
break;
}
答案 2 :(得分:0)
你实际上可以使用括号。
switch (operation) {
case "divide": {
double result = (double) first / second;
string = Double.toString(result1);
break;
}
case "multiply": {
double result = (double) first * second;
string = Double.toString(result2);
break;
}
default:
break;
}
答案 3 :(得分:0)
在java中,{}中定义的任何局部变量都可用到最后一行,因此您可以在块内定义之后重用任何位置。所以在你的情况下没有用来重新定义结果变量。只需在第一行定义并随处使用。
double result = 0;
switch (operation) {
case "divide":
result = (double) first / second;
string = Double.toString(result1);
break;
case "multiply":
result = (double) first * second;
string = Double.toString(result2);
break;
case "substract":
result = (double) first - second;
string = Double.toString(result3);
break;
case "add":
result = (double) first + second;
string = Double.toString(result4);
break;
}
答案 4 :(得分:0)
因为你已经定义了双重结果变量,所以就像下面那样希望它能起作用
double result = 0; <----- already define here
switch (operation) {
case "divide":
result = (double) first / second; <------ there is no need to
define it again
string = Double.toString(result1);
break;
case "multiply":
result = (double) first * second;
string = Double.toString(result2);
break;
case "substract":
result = (double) first - second;
string = Double.toString(result3);
break;
case "add":
result = (double) first + second;
string = Double.toString(result4);
break;
}
答案 5 :(得分:0)
如果你想使用一个公共变量,你可以这样做。
double result = 0;
switch (operation) {
case "divide":
result = (double) first / second;
string = Double.toString(result);
break;
case "multiply":
result = (double) first * second;
string = Double.toString(result);
break;
case "substract":
result = (double) first - second;
string = Double.toString(result);
break;
case "add":
result = (double) first + second;
string = Double.toString(result);
break;
}