繁体   English   中英

在Java中,是否可以在每种情况下继续跟踪switch语句中的所有值?

[英]in Java, can I keep tracking all the value in a switch statement for each 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;

            }

您可以在循环中执行相同的操作,而不用使用切换用例,因为在这种情况下您没有做任何不同的事情-

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" );
}

该代码基于以下假设:您拥有要在其中调用开关盒的产品列表...整个代码都可以替换为...如果您需要将特定产品与特定编号关联,则您可以可以使用Enum并在循环内在此处使用它。

从您的问题中不清楚您要寻找的是什么,但我会猜测。 如果您试图获得五个项目中每一项的行总数以及总数,则可以使用一个数组作为行总数,该数组在switch语句之前定义:

double[] lineTotals = new double[5];
double orderTotal = 0;

然后,您可以将数组中每个项目的值放入,记住索引从0开始而不是1:

switch(productNo) {
    case 1:
        lineAmounts[0] = quantity * products[0];
        orderAmount = +lineAmounts[0];
        textArea.append(productNo +"\t"
            + quantity + "\t" 
            + "$" + lineAmount1 +"\t"
            + "$" + orderAmount + "\n" );
        break;

    case 2: ... etc ...
}

暂无
暂无

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

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