如何将我的哨兵带出循环?

时间:2018-06-05 01:12:23

标签: java while-loop

我想知道我是否可以获得一些关于如何将我的哨兵带出循环的反馈,因此它不算作我答案的一部分。我尝试在我的while循环中添加一个中断,但它只会弄乱我的公式。 (我要做的是让系统打印佣金的总有效条目,并为所有条目提供总计)。感谢

public class Commission {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        NumberFormat nf = NumberFormat.getCurrencyInstance();

        int salesPeople = 0;
        double baseSalary = 200;
        double sales = 0;
        double salesTotal = 0;
        double grandTotal = 0;
        while (sales != -999) {
            // ask for the amount of sales
            System.out.println("Enter sales amount (enter -999 to end): ");
            sales = scan.nextDouble();
            salesPeople = salesPeople + 1;

            // do the calculations
            double noCommission = 0;
            double lowCommission = ((sales - 500) * .03);
            double mediumCommission = ((500 * .03) + ((sales - 1000) * .05));
            double highCommission = ((500 * .03) + (2000 * .05) + ((sales - 3000) * .08));

            double commission = 0;

            if (sales < 500)
                commission = noCommission;
            else if (sales > 500 && sales < 1000)
                commission = lowCommission;
            else if (sales > 1000 && sales < 3000)
                commission = mediumCommission;
            else if (sales >= 3000)
                commission = highCommission;

            salesTotal = baseSalary + commission;
            grandTotal = grandTotal + salesTotal;

            System.out.println("You earned " + nf.format(commission)
                + " in commission for a total $" + salesTotal
                + " salary.\n");

        }
        if (sales == -999){
            System.out.println("The number of employees is " + salesPeople
                + " and the total payout is " + grandTotal);
        }

    }

}

1 个答案:

答案 0 :(得分:1)

输入后立即对哨兵值进行测试。您正在计算销售数量(而不是员工数量),我将限制变量范围(和蠕变)。像,

Scanner scan = new Scanner(System.in);
NumberFormat nf = NumberFormat.getCurrencyInstance();
int salesCount = 0;
double grandTotal = 0, baseSalary = 200;
while (true) {
    // ask for the amount of sales
    System.out.println("Enter sales amount (enter -999 to end): ");
    double sales = scan.nextDouble();
    if (sales == -999) {
        break;
    }
    salesCount++;
    double commission = 0;
    if (sales >= 500 && sales < 1000) {
        commission = ((sales - 500) * .03);
    } else if (sales >= 1000 && sales < 3000) {
        commission = ((500 * .03) + ((sales - 1000) * .05));
    } else if (sales >= 3000) {
        commission = ((500 * .03) + (2000 * .05) + ((sales - 3000) * .08));
    }

    double salesTotal = baseSalary + commission;
    grandTotal += salesTotal;

    System.out.println("You earned " + nf.format(commission) 
            + " in commission for a total $"
            + nf.format(salesTotal) + " salary.\n");
}
System.out.println("The number of sales is " + salesCount 
        + " and the total payout is " + nf.format(grandTotal));