小编Fyr*_*ree的帖子

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

我如何将在此 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;
        }
Run Code Online (Sandbox Code Playgroud)

在 switch 语句的外面,我希望一旦用户停止使用 switch 语句(我在 while 循环中使用它)就将 FiveTotal 添加到一起,然后希望返回五个总计以在 main 方法中使用类(switch 语句位于其自己的双精度、非 void 类中,因此它可以返回值)。

java double user-input return switch-statement

5
推荐指数
2
解决办法
3万
查看次数

标签 统计

double ×1

java ×1

return ×1

switch-statement ×1

user-input ×1