I still writing the code, and it doesn't make a very big difference in a project like mine, but if I was to make something bigger, it would be a pain. here it is:
我仍在编写代码,并且它在像我这样的项目中没有产生很大的不同,但如果我要做更大的事情,那将是一个痛苦。这里是:
case 0:
System.out.print("Insert the N: ");
double N = in.nextDouble();
double mol = N / Na;
System.out.print("There are " + mol + " mol in that sample");
break;
case 1:
System.out.print("Insert the m: ");
double m = in.nextDouble();
System.out.print("Insert the M: ");
double M = in.nextDouble();
double mol = m / M;
System.out.print("There are " + mol + " mol in that sample");
break;
case 2:
System.out.print("Insert the V: ");
double V = in.nextDouble();
double mol = V / Vm;
System.out.print("There are " + mol + " mol in that sample");
break;
The first "mol" has no problem, but in case 1 and case 2, it says "Duplicate local variable mol". If I use the If statement it works. Is Java just like this or there is a way around it?
第一个“mol”没有问题,但在案例1和案例2中,它表示“重复局部变量mol”。如果我使用If语句就可以了。 Java是这样还是有办法解决它?
Thanks
谢谢
2 个解决方案
#1
14
That's because a case
doesn't create a scope. So, both the variables in the 2 cases are in the same scope. If you want to do this, you can add braces for each case, which will create a new scope for each case.
那是因为案例没有创建范围。因此,2个案例中的两个变量都在同一范围内。如果要执行此操作,可以为每个案例添加大括号,这将为每个案例创建一个新范围。
case 0: {
System.out.print("Insert the N: ");
double N = in.nextDouble();
double mol = N / Na;
System.out.print("There are " + mol + " mol in that sample");
break;
}
case 1: {
System.out.print("Insert the m: ");
double m = in.nextDouble();
System.out.print("Insert the M: ");
double M = in.nextDouble();
double mol = m / M;
System.out.print("There are " + mol + " mol in that sample");
break;
}
But, ideally there is no need to declare a separate local variable for each case. If you are using a variable in all the cases, that clearly signifies that variable to be declared directly inside the switch
statement:
但是,理想情况下,不需要为每种情况声明一个单独的局部变量。如果在所有情况下都使用变量,那么这清楚地表示要在switch语句中直接声明的变量:
switch (someVar) {
double mol = 0.0;
case 0: mol = n / Na;
break;
case 1: mol = m / M;
break;
}
P.S.: Can I advice you to name your variables something apart from english alphabets - n
, M
, N
?
P.S。:我可以建议你将变量命名为英文字母 - n,M,N吗?
#2
4
Because those variables are there in single block
may be you would have written this switch
statement in some method
. In a single method you can't have dup variables.
因为那些变量存在于单个块中,您可能会在某些方法中编写此switch语句。在单一方法中,您不能拥有重复变量。
#1
14
That's because a case
doesn't create a scope. So, both the variables in the 2 cases are in the same scope. If you want to do this, you can add braces for each case, which will create a new scope for each case.
那是因为案例没有创建范围。因此,2个案例中的两个变量都在同一范围内。如果要执行此操作,可以为每个案例添加大括号,这将为每个案例创建一个新范围。
case 0: {
System.out.print("Insert the N: ");
double N = in.nextDouble();
double mol = N / Na;
System.out.print("There are " + mol + " mol in that sample");
break;
}
case 1: {
System.out.print("Insert the m: ");
double m = in.nextDouble();
System.out.print("Insert the M: ");
double M = in.nextDouble();
double mol = m / M;
System.out.print("There are " + mol + " mol in that sample");
break;
}
But, ideally there is no need to declare a separate local variable for each case. If you are using a variable in all the cases, that clearly signifies that variable to be declared directly inside the switch
statement:
但是,理想情况下,不需要为每种情况声明一个单独的局部变量。如果在所有情况下都使用变量,那么这清楚地表示要在switch语句中直接声明的变量:
switch (someVar) {
double mol = 0.0;
case 0: mol = n / Na;
break;
case 1: mol = m / M;
break;
}
P.S.: Can I advice you to name your variables something apart from english alphabets - n
, M
, N
?
P.S。:我可以建议你将变量命名为英文字母 - n,M,N吗?
#2
4
Because those variables are there in single block
may be you would have written this switch
statement in some method
. In a single method you can't have dup variables.
因为那些变量存在于单个块中,您可能会在某些方法中编写此switch语句。在单一方法中,您不能拥有重复变量。