在“do...while”循环中出现“while expected”错误

Posted

技术标签:

【中文标题】在“do...while”循环中出现“while expected”错误【英文标题】:Getting "while expected" error in "do...while" loop 【发布时间】: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");
            
         
         
    
    

【问题讨论】:

你得到什么错误? 【参考方案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 中的变量通常以小写字母开头。这不是一个严格的规则,而是一个谨慎遵守的约定。

【讨论】:

以上是关于在“do...while”循环中出现“while expected”错误的主要内容,如果未能解决你的问题,请参考以下文章

sql server中do while循环怎么写

C语言 do while 和 while 循环

简析Java中while循环与do/while循环的区别

循环语句(while,for,do...while)

Java 循环结构 - for, while 及 do...while

Java 循环结构 - for, while 及 do...while