我在下面的break语句运行正常。没有错误。但是,当我为多个产品输入多个值时,它仅返回最后输入的产品的数据。例如,如果我输入productNum = 1且numSold = 5,则productNum = 2,则numSold = 5;它将仅计算productNum = 2的totalSales,并且仅跟踪返回值numSold =5。它将“忘记” productNum = 1及其numSold的数据。

我希望它存储numSold1到numSold5的数据,并且还要继续向totalSales添加值。任何人都可以为此指出正确的方向吗?

这是我的销售计算器:

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


输入内容如下:

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

最佳答案

根据您设计程序的方式,您需要在每两次调用calculateSales()setProductNum()之后调用setNumSold()

因此,以下行需要放在您的do ... while循环中:

salesCalculator1.calculateSales();


这样做的原因是productNumnumSold被对setProductNum()setNumSold()的每对调用覆盖。因此,您需要在它们为最新时使用这些值。否则,您将注意到,所有计算将使用最后的设置值,即。较早的值是“忘记”。

关于java - Break语句不存储数据,仅返回最后一个值输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46800980/

10-11 11:14