Java 我怎样才能把我的哨兵带出圈外?

Java 我怎样才能把我的哨兵带出圈外?,java,while-loop,Java,While Loop,我想知道我是否能得到一些关于如何让我的哨兵退出循环的反馈,这样它就不会被算作我答案的一部分。我试着在我的while循环中添加一个中断,但它只会弄乱我的公式。(我试图做的是让系统打印所有有效的佣金条目,并提供所有条目的总计)。谢谢 公共类委员会{ 公共静态void main(字符串[]args){ 扫描仪扫描=新扫描仪(System.in); NumberFormat nf=NumberFormat.getCurrencyInstance(); int销售人员=0; 双基工资=200; 双倍销售额

我想知道我是否能得到一些关于如何让我的哨兵退出循环的反馈,这样它就不会被算作我答案的一部分。我试着在我的while循环中添加一个中断,但它只会弄乱我的公式。(我试图做的是让系统打印所有有效的佣金条目,并提供所有条目的总计)。谢谢

公共类委员会{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
NumberFormat nf=NumberFormat.getCurrencyInstance();
int销售人员=0;
双基工资=200;
双倍销售额=0;
双倍销售总额=0;
总双倍=0;
while(销售!=-999){
//询问销售额
System.out.println(“输入销售金额(输入-999结束):”;
sales=scan.nextDouble();
销售人员=销售人员+1;
//计算
双重无共同责任=0;
双低佣金=((销售额-500)*.03);
双介质佣金=(500*.03)+(销售额-1000)*.05);
双高佣金=((500*.0.03)+(2000*.0.05)+(销售额-3000)*.08));
双重佣金=0;
如果(销售额<500)
佣金=无佣金;
否则如果(销售额>500和销售额<1000)
佣金=低佣金;
否则如果(销售额>1000和销售额<3000)
佣金=中等佣金;
否则如果(销售额>=3000)
佣金=高额佣金;
salesTotal=基本工资+佣金;
grandTotal=grandTotal+salesTotal;
System.out.println(“您赢得的”+nf.format(佣金)
+“佣金总额$”+销售总额
+“工资。\n”);
}
如果(销售额==-999){
System.out.println(“员工人数为”+销售人员
+“且总支出为”+grandTotal);
}
}
}

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

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));
Scanner scan=新的扫描仪(System.in);
NumberFormat nf=NumberFormat.getCurrencyInstance();
int salescont=0;
双倍总薪=0,底薪=200;
while(true){
//询问销售额
System.out.println(“输入销售金额(输入-999结束):”;
双重销售=scan.nextDouble();
如果(销售额==-999){
打破
}
salesCount++;
双重佣金=0;
如果(销售额>=500和销售额<1000){
佣金=((销售额-500)*.03);
}否则如果(销售额>=1000和销售额<3000){
佣金=((500*.03)+(销售额-1000)*.05));
}否则如果(销售额>=3000){
佣金=((500*.03)+(2000*.05)+(销售额-3000)*.08));
}
双倍销售总额=基本工资+佣金;
grandTotal+=销售总额;
System.out.println(“您赢得的”+nf.format(佣金)
+“佣金总额为美元”
+nf.格式(salesTotal)+“工资。\n”);
}
System.out.println(“销售数量为”+salesCount
+“总支出为”+nf.format(grandTotal));

最好使用sentinel而不是while(正确)…然后中断。通过谷歌搜索:
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));