简体   繁体   English

Break语句不存储数据,仅返回最后一个值输入

[英]Break statement not storing data, returns only last value input

My break statement below works perfectly fine. 我在下面的break语句工作得很好。 There are no errors. 没有错误。 However, when I enter multiple values for multiple products, it only returns data for the last product entered. 但是,当我为多个产品输入多个值时,它仅返回最后输入的产品的数据。 For example, if I input productNum = 1 and numSold = 5, then productNum = 2, then numSold = 5; 例如,如果我输入productNum = 1且numSold = 5,则productNum = 2,则numSold = 5; it will only calculate totalSales for productNum = 2 and only track return a value numSold = 5. It will "forget" the data for productNum = 1 and its numSold. 它将仅计算productNum = 2的totalSales,并且仅跟踪返回值numSold =5。它将“忘记” productNum = 1及其numSold的数据。

I want it to store data for numSold1 thru numSold5, and also continue to add values to totalSales. 我希望它存储numSold1到numSold5的数据,并且还要继续向totalSales添加值。 Can anyone please point in the right direction for this? 任何人都可以为此指出正确的方向吗?

Here's my sales calculator: 这是我的销售计算器:

public class SalesCalculator {

    private int productNum, numSold, numSold1, numSold2, numSold3, numSold4, numSold5;

    private double price1 = 2.98, price2 = 4.50, price3 = 9.98, price4 = 4.49, price5 = 6.87, totalSales = 0.00;

    public int setProductNum(int productNum) {

        this.productNum = productNum;
        return productNum;
    }

    public int setNumSold(int numSold) {

        this.numSold = numSold;
        return numSold;
    }

    public double calculateSales() {

            switch (productNum) {

                case 1:
                    totalSales += (price1 * numSold); // multiplies how much of this product was sold by its price and adds to overall sales
                    numSold1 += numSold; // tracks how much of this product was sold of this product
                    break;

                case 2:
                    totalSales += (price2 * numSold);
                    numSold2 += numSold;
                    break;

                case 3:
                    totalSales += (price3 * numSold);
                    numSold3 += numSold;
                    break;

                case 4:
                    totalSales += (price4 * numSold);
                    numSold4 += numSold;
                    break;

                case 5:
                    totalSales += (price5 * numSold);
                    numSold5 += numSold;
                    break;

                // default just breaks out of loop in case invalid product number entered
                default:
                    break;
            }

        return totalSales;
    }

public double getTotalSales() { return totalSales; }

public int getNumSold1() { return numSold1; }
public int getNumSold2() { return numSold2; }
public int getNumSold3() { return numSold3; }
public int getNumSold4() { return numSold4; }
public int getNumSold5() { return numSold5; }

// get prices, included because i wasn't sure if assignment required them or not
public double getPrice1() { return price1; }
public double getPrice2() { return price2; }
public double getPrice3() { return price3; }
public double getPrice4() { return price4; }
public double getPrice5() { return price5; }
}

Here's what takes the input: 输入内容如下:

public class SalesCalculatorTest {

public static void main(String[] args){

    SalesCalculator salesCalculator1 = new SalesCalculator();

    Scanner input = new Scanner(System.in);
    int calcSales;

    do {

        System.out.printf("\nPlease enter the product number, then press ENTER.\n");
        int productNum = input.nextInt();
        salesCalculator1.setProductNum(productNum);

        System.out.printf("\nPlease enter the quantity sold, then press ENTER.\n");
        int numSold = input.nextInt();
        salesCalculator1.setNumSold(numSold);

        System.out.printf("\nType -1 to calculate sales. Press ENTER to input more data.\n");
        calcSales = input.nextInt();
        } while (calcSales != -1);

    if (calcSales == -1) {

        salesCalculator1.calculateSales();
    }

    double totalSales = salesCalculator1.getTotalSales();
    System.out.printf("\nTotal sales for products sold was $%.2f", totalSales);

    int numSold1 = salesCalculator1.getNumSold1();
    double price1 = salesCalculator1.getPrice1();
    System.out.printf("\n\nProduct 1 sold %d units at $%.2f each", numSold1, price1);

    int numSold2 = salesCalculator1.getNumSold2();
    double price2 = salesCalculator1.getPrice2();
    System.out.printf("\nProduct 2 sold %d units at $%.2f each", numSold2, price2);

    int numSold3 = salesCalculator1.getNumSold3();
    double price3 = salesCalculator1.getPrice3();
    System.out.printf("\nProduct 3 sold %d units at $%.2f each", numSold3, price3);

    int numSold4 = salesCalculator1.getNumSold4();
    double price4 = salesCalculator1.getPrice4();
    System.out.printf("\nProduct 4 sold %d units at $%.2f each", numSold4, price4);

    int numSold5 = salesCalculator1.getNumSold5();
    double price5 = salesCalculator1.getPrice5();
    System.out.printf("\nProduct 5 sold %d units at $%.2f each\n", numSold5, price5);
}
}

According to the way you have designed your program, you need to call calculateSales() after each pair of calls to setProductNum() and setNumSold() . 根据你设计你的程序的方式,你需要调用calculateSales()每一对呼叫后setProductNum()setNumSold()

Therefore the following line needs to be within your do ... while loop: 因此,以下行需要在do ... while循环中:

salesCalculator1.calculateSales();

The reason for this is that productNum and numSold are over-written with each pair of calls to setProductNum() and setNumSold() . 这样做的原因是, productNumnumSold被盖写与每对的呼叫到setProductNum()setNumSold() Therefore you need to use these values while they are current. 因此,您需要在当前使用这些值。 Otherwise, as you note, all your calculations will use the last set values, ie. 否则,您将注意到,所有计算将使用最后设置的值,即。 the earlier values are "forgotten". 较早的值被“忘记”。

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

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