如何返回在 switch 语句中找到的值 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28547587/
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
How to return values found in a switch statement Java
提问by Fyree
How would I return the values that I found in this switch statement to the rest of the class for use (I want to use oneTotal, twoTotal, etc. out side of that switch statement)? Here is the code for the switch statement:
我如何将我在这个 switch 语句中找到的值返回给类的其余部分使用(我想在该 switch 语句之外使用 oneTotal、twoTotal 等)?这是 switch 语句的代码:
switch(itemNo){
case 1:
double oneTotal = 2.98 * userQuantity;
return oneTotal;
case 2:
double twoTotal = 4.50 * userQuantity;
return twoTotal;
case 3:
double threeTotal = 9.98 * userQuantity;
return threeTotal;
case 4:
double fourTotal = 4.49 * userQuantity;
return fourTotal;
case 5:
double fiveTotal = 6.87 * userQuantity;
return fiveTotal;
}
Out side of the switch statement I want the fiveTotal's to be added up once the user's stops using the switch statement (I have it in a while loop), and then want the five totals to be returned to be used in the main method of the class (the switch statement is in its own double, non void class so it can return values).
在 switch 语句之外,我希望一旦用户停止使用 switch 语句(我在 while 循环中使用它)就将五个总计加起来,然后希望返回五个总计以在主方法中使用类(switch 语句在它自己的双精度非 void 类中,因此它可以返回值)。
回答by SMA
Just try defining a variable called result of double type before switch statement like:
只需尝试在 switch 语句之前定义一个称为 double 类型的变量,例如:
double result = 0.;
switch(itemNo){
case 1:
result = 2.98 * userQuantity;
break;
....
}
return result;
回答by Luiggi Mendoza
The method must only contain the necessary code to do its work. The consumers of the method must worry about how to work with the data.
该方法必须只包含完成其工作所需的代码。该方法的使用者必须担心如何处理数据。
For your case, your method containing this switch
must only care about returning the proper value based on the parameter(s), and the clients of the method e.g. public static void main(String[] args)
must evaluate what to do with the results from executing this method.
对于您的情况,您的包含 this 的方法switch
必须只关心根据参数返回正确的值,并且该方法的客户端例如public static void main(String[] args)
必须评估如何处理执行此方法的结果。
Here's how this may work for your case:
以下是这可能适用于您的情况的方式:
public static double yourMethod(int itemNo) {
double result = 0;
switch(itemNo) {
case 1:
result = 2.98 * userQuantity;
break;
case 2:
result = 4.50 * userQuantity;
break;
case 3:
result = 9.98 * userQuantity;
break;
case 4:
result = 4.49 * userQuantity;
break;
case 5:
result = 6.87 * userQuantity;
break;
}
return result;
}
public static void main(String[] args) {
//some code...
double grandTotal = 0;
//probably this may be in a loop or something similar
int itemNo = ...;
//this method is consumer/client of yourMethod, directly noted by line below
double someValue = yourMethod(itemNo);
grandTotal += someValue;
//more code...
//do something with the result e.g. print it so the user may read it
System.out.println("Grand total: " + grandTotal);
}
回答by Yang Yu
You can create a variable out side the switch statement and is visible for rest of the class to keep track of the return values:
您可以在 switch 语句之外创建一个变量,该变量对类的其余部分可见,以跟踪返回值:
public double sumFives = 0; //global variable
/* some codes */
switch(itemNo){
case 1:
double oneTotal = 2.98 * userQuantity;
return oneTotal;
case 2:
double twoTotal = 4.50 * userQuantity;
return twoTotal;
case 3:
double threeTotal = 9.98 * userQuantity;
return threeTotal;
case 4:
double fourTotal = 4.49 * userQuantity;
return fourTotal;
case 5:
double fiveTotal = 6.87 * userQuantity;
sumFives += fiveTotal;
return fiveTotal;
}
}
There, you'll have access to the sum after the switch statement. If you want to track each individual return values, simple use an ArrayList.
在那里,您可以访问 switch 语句后的总和。如果要跟踪每个单独的返回值,只需使用 ArrayList。
Hope this helps :)
希望这可以帮助 :)
Yang
杨
回答by Hermann Klecker
double returnValue;
switch(itemNo){
case 1:
double oneTotal = 2.98 * userQuantity;
returnValue = oneTotal;
break;
case 2:
double twoTotal = 4.50 * userQuantity;
returnValue = twoTotal;
break;
case 3:
double threeTotal = 9.98 * userQuantity;
returnValue = threeTotal;
break;
case 4:
double fourTotal = 4.49 * userQuantity;
returnValue = fourTotal;
break;
case 5:
double fiveTotal = 6.87 * userQuantity;
returnValue = fiveTotal;
break;
}
return returnValue;
or
或者
double factor;
switch(itemNo){
case 1:
factor = 2.98;
break;
case 2:
factor = 4.50;
break;
case 3:
factor = 9.98;
break;
case 4:
factor = 4.49;
break;
case 5:
factor = 6.87;
break;
}
return factor * userQuantity;
And don't forget the default branch. Throw an exception at least.
并且不要忘记默认分支。至少抛出一个异常。
or
或者
double[] myItemValues= {2.98, 4.5, 9.98, 4.49, 6.87};
return myItemValue[itemNo];
回答by someone
Define a variable on top of switch statement so that it is visible for switch scope. then assign your value to that variable in each case. Then as last line return your assigned variable.
在 switch 语句之上定义一个变量,以便它在 switch 范围内可见。然后在每种情况下将您的值分配给该变量。然后作为最后一行返回您分配的变量。
回答by Surabhi Raje
There are two ways you can go about this depending on what you want to do. First, If you want to just keep adding the sum, you can define a variable outside the function and keep incrementing it.
有两种方法可以解决此问题,具体取决于您想要做什么。首先,如果您只想继续添加总和,您可以在函数外部定义一个变量并继续增加它。
Eg:
double result = 0.0;
while(*your condition*){
//Take itemNo from user
switch(itemNo){
case 1:
result += 2.98 * userQuantity; //The += ensures you are cumulating
break;
....
}
}
return result;
Or, If you want to have those values distinct then you can keep an array[5]
或者,如果你想让这些值不同,那么你可以保留一个数组[5]
double result[5]={0.0, 0.0, 0.0, 0.0, 0.0};
switch(itemNo){
case 1:
result[0] = 2.98 * userQuantity;
break;
case 2:
result[1] = 4.50 * userQuantity;
break;
case 3:
result[2] = 9.98 * userQuantity;
break;
case 4:
result[3] = 4.49 * userQuantity;
break;
case 5:
result[4] = 6.87 * userQuantity;
break;
}
return result;
This was you will can have explicit knowledge outside your switch-case of what the user entered and do any manipulation with the data.
这是您可以在切换案例之外获得关于用户输入内容的明确知识,并对数据进行任何操作。
回答by Guillaume F.
On Java 14, you can use
在 Java 14 上,您可以使用
double total = switch(itemNo){
case 1 -> 2.98 * userQuantity;
case 2 -> 4.50 * userQuantity;
case 3 -> 9.98 * userQuantity;
case 4 -> 4.49 * userQuantity;
case 5 -> 6.87 * userQuantity;
};
回答by EpicPandaForce
The following should work:
以下应该工作:
double total;
switch(itemNo){
case 1:
total = 2.98 * userQuantity;
break;
case 2:
total = 4.50 * userQuantity;
break;
case 3:
total = 9.98 * userQuantity;
break;
case 4:
total = 4.49 * userQuantity;
break;
case 5:
total = 6.87 * userQuantity;
break;
default:
throw new IllegalArgumentException("selected: " + itemNo);
}
return total;
回答by Dulith De Costa
double value = 0.0;
switch(itemNo){
case 1:
double oneTotal = 2.98 * userQuantity;
value = oneTotal;
break;
case 2:
double twoTotal = 4.50 * userQuantity;
value = twoTotal;
break;
case 3:
double threeTotal = 9.98 * userQuantity;
value = threeTotal;
break;
case 4:
double fourTotal = 4.49 * userQuantity;
value = fourTotal;
case 5:
double fiveTotal = 6.87 * userQuantity;
value = fiveTotal;
break;
}
return value;
回答by Reger05
I'm not sure the implementation you are looking to use this in....however it seems like you are on the right track.
我不确定您希望在...中使用它的实现方式。但是,您似乎走在正确的轨道上。
Simply wrap your switch statement into a method
只需将 switch 语句包装成一个方法
...
double total = 0;
while(loopControl){
total += getSwitch(switchValue);
}
....
public double getSwitch(switchValue){
double returnValue = 0;
switch(switchValue){
case 1:
returnvalue = *your formula*;
break;
....
}
return returnValue;
}
edit for passing in argument for whatever your switch value will beapologies...
编辑传递参数,无论您的开关值是什么,都会道歉...