【问题标题】:Getting "while expected" error in "do...while" loop在“do...while”循环中出现“while expected”错误
【发布时间】:2016-07-30 19:51:44
【问题描述】:

我不断收到这个错误,我没有被困在试图修复它。

package bonuscalc;

import java.text.DecimalFormat;
import java.util.Scanner;

public class BonusCalc {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    
         Scanner input = new Scanner(System.in); 
         DecimalFormat formatter = new DecimalFormat("#0.00");
        
         int Salary;
         double NewSal, Comm;
         double p1 = 0.1;
         double p2 = 0.15;
         double p3 = 0.2;
         double p4 = 0.3;
        
         System.out.println("Welcome to Bonus Calculator");
         
         do{
            System.out.print("Enter your Salary: ");
            Salary = input.nextInt();
           }While (Salary < 0)
    
         if((Salary >  0) && (Salary <= 8000)){
             Comm = (Salary  * p1);
             NewSal = Salary + Comm;
             System.out.print("Your Commition is RM" + formatter.format(Comm)); 
             System.out.println(" and your New Salary is RM" + formatter.format(NewSal));
        
        }
          else if((Salary >  8000) && (Salary <= 15000)){
             Comm = (Salary  * p2);
             NewSal = Salary + Comm;
             System.out.print("Your Commition is RM" + formatter.format(Comm)); 
             System.out.println(" and your New Salary is RM" + formatter.format(NewSal));
            
        }
         else if((Salary >  15000) && (Salary <= 25000)){
             Comm = (Salary  * p3);
             NewSal = Salary + Comm;
             System.out.print("Your Commition is RM" + formatter.format(Comm)); 
             System.out.println(" and your New Salary is RM" + formatter.format(NewSal));
    
        }
         else if(Salary >  25000){
             Comm = (Salary  * p4);
             NewSal = Salary + Comm;
             System.out.print("Your Commition is RM" + formatter.format(Comm)); 
             System.out.println(" and your New Salary is RM" + formatter.format(NewSal));
             
        }
         else{
             System.out.println("Input invalid. Renter Salary");
            
         }
         
    }
    
}

【问题讨论】:

  • 你得到什么错误?

标签: java while-loop


【解决方案1】:

你写的是While而不是while

do {
...
} While (Salary < 0);

正确的是:

do {
...
} while (Salary < 0);

希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    您的do-while 循环语法无效。首先,while 是小写的,因此While 是不正确的。此外,您缺少分号。

    do {
      System.out.print("Enter your Salary: ");
      Salary = input.nextInt();
    } while (Salary < 0);
    

    附带说明,Java 中的变量通常以小写字母开头。这不是一个严格的规则,而是一个谨慎遵守的约定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2023-03-18
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多