虽然预期错误

时间:2016-04-09 14:51:14

标签: java while-loop

我一直收到这个错误,我并没有试图修复它。

package bonuscalc;

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

公共类BonusCalc {     / **      * @param args命令行参数      * /     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");

     }

}

}

2 个答案:

答案 0 :(得分:2)

您已撰写While而不是while

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

正确的是:

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

希望这能解决你的问题。

答案 1 :(得分:1)

您的do-while循环语法无效。首先,while是小写的,因此While不正确。而且,你错过了一个分号。

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

另外,在Java变量中,通常以小写字母开头。这不是一个严格的规则,而是一个谨慎遵守的惯例。