简体   繁体   English

如何返回在 switch 语句中找到的值 Java

[英]How to return values found in a switch statement Java

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)?我如何将我在这个 switch 语句中找到的值返回给类的其余部分以供使用(我想在该 switch 语句之外使用 oneTotal、twoTotal 等)? Here is the code for the switch statement:这是 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 类中,因此它可以返回值)。

Just try defining a variable called result of double type before switch statement like:只需尝试在 switch 语句之前定义一个名为 result 的 double 类型变量,例如:

double result = 0.;
switch(itemNo){
    case 1: 
        result = 2.98 * userQuantity; 
        break;
     ....
}
return result;

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 eg public static void main(String[] args) must evaluate what to do with the results from executing this method.对于您的情况,包含此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);
}

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.在那里,您可以访问 switch 语句后的总和。 If you want to track each individual return values, simple use an ArrayList.如果要跟踪每个单独的返回值,只需使用 ArrayList。

Hope this helps :)希望这可以帮助 :)

Yang

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;
        };
    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];

Define a variable on top of switch statement so that it is visible for switch scope.在 switch 语句之上定义一个变量,以便它在 switch 范围内可见。 then assign your value to that variable in each case.然后在每种情况下将您的值分配给该变量。 Then as last line return your assigned variable.然后作为最后一行返回您分配的变量。

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;
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;

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 be apologies...编辑以传递参数,无论您的开关值将是道歉...

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.这是您可以在切换案例之外获得有关用户输入内容的明确知识,并对数据进行任何操作。

This can also be written as mentioned below,这也可以写成下面提到的,

switch(itemNo){
        case 1:
            return 2.98 * userQuantity;
        case 2:
            return 4.50 * userQuantity;
        case 3:
            return 9.98 * userQuantity;
        case 4:
            return 4.49 * userQuantity;
        case 5:
            return 6.87 * userQuantity;
        }
        return 0; //default value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM