java - 在Java中,我可以跟踪每种情况的switch语句中的所有值吗?

标签 java switch-statement case

我刚刚开始学习Java,我需要使用switch语句,但是我需要跟踪每种情况的所有计算值,然后需要将其相加。我该怎么做?

这是我现在的代码

            switch(productNo)
            {
                case 1:
                    lineAmount1 = quantity * product1;
                    orderAmount = +lineAmount1;
                    textArea.append(productNo +"\t"
                            + quantity + "\t" 
                            + "$" + lineAmount1 +"\t"
                            + "$" + orderAmount + "\n" );


                    break;
                case 2:
                    lineAmount2 = quantity * product2;
                    orderAmount = + lineAmount2;
                    textArea.append(productNo +"\t"
                            + quantity + "\t" 
                            + "$" + lineAmount2 +"\t"
                            + "$" + orderAmount + "\n" );


                    break;

                case 3:
                    lineAmount3 = quantity * product3;
                    orderAmount = +lineAmount3;
                    textArea.append(productNo +"\t"
                            + quantity + "\t" 
                            + "$" + lineAmount3 +"\t"
                            + "$" + orderAmount + "\n" );

                    break;

                case 4:
                    lineAmount4 = quantity * product4;
                    orderAmount = +lineAmount4;
                    textArea.append(productNo +"\t"
                            + quantity + "\t" 
                            + "$" + lineAmount4 +"\t"
                            + "$" + orderAmount + "\n" );

                    break;

                case 5:
                    lineAmount5 = quantity * product5;
                    orderAmount = +lineAmount5;
                    textArea.append(productNo +"\t"
                            + quantity + "\t" 
                            + "$" + lineAmount5 +"\t"
                            + "$" + orderAmount);

                    break;

            }

最佳答案

您可以在循环中执行相同的操作,而不是使用 switch-case,因为您在情况下没有做任何不同的事情 -

int lineAmount = 0;
int orderAmount = 0;
for (int product : products) {
lineAmount = quantity * product;
orderAmount += lineAmount;
textarea.append(productNo +"\t"
                        + quantity + "\t" 
                        + "$" + lineAmount +"\t"
                        + "$" + orderAmount + "\n" );
}

此代码基于这样的假设:您有一个在其中调用 switch case 的产品列表...整个代码可以用此替换... 如果您需要将特定产品与特定数字相关联,那么您可以使用 Enum 并在此处使用它,而不是在循环内使用。

关于java - 在Java中,我可以跟踪每种情况的switch语句中的所有值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17125735/

相关文章:

java - ActionBarActivity 无法解析。无法启动需要 Theme.AppCompat 的 Activity 。修复 Android 应用中的依赖关系

java - 如何将 JobParameters 传递给 Spring Batch Junit 测试用例 JobLauncherTestUtils?

java - JDBC sql查询(批处理)

c++ - 使用 case switch 语句加载函数

SQL Server : use CASE with LIKE

C 编程开关案例

variables - 在 switch 语句 powershell 中测试 4 个不同的变量

java - 完成 JavaFX 场景后如何返回代码

java - 替换嵌套开关/ifelse 的设计模式

c# - 如何从方法的 switch 语句返回不确定类型的值