2014-09-18 12 views
-2

我是新來的java和我堅持非常簡單的代碼。我已經添加了while條件,但日食仍然要求它。我是一個新的Java。我已經添加了while條件,但日食仍然要求它

錯誤:

Syntax error, insert while (Expression); to complete BlockStatements

代碼:

package assignment517; 
import java.util.Scanner; 

public class Products { 
    public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    do{ 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter the number of product"); 
     int numberOfProduct=input.nextInt(); 
     System.out.print("Enter the products sold"); 
     int productsSold=input.nextInt(); 

     double pprice = 1.0; 

     switch(numberOfProduct){ 
     case 1: { 
      System.out.print("Price of total products sold"); 
      System.out.print (pprice= 2.98*productsSold); 
      break; 
     } 

     case 2: { 
      System.out.print("Price of total products sold");   
      System.out.print(pprice= 4.50*productsSold); 
      break; 
     } 

     case 3: { 
      System.out.print("Price of total products sold"); 
      System.out.print(pprice= 9.98*productsSold); 
      break; 
     } 

     case 4: { 
      System.out.print("Price of total products sold"); 
      System.out.print(pprice= 4.49*productsSold); 
      break; 
     } 

     case 5:{ 
      System.out.print("Price of total products sold"); 
      System.out.print(pprice= 6.87*productsSold); 
      break; 
     } 

     default:{ 
      System.out.print("invalid value"); 
     } 

     }while(numberOfProduct !=6); 
    } 
    } 
+0

你可以粘貼它給你的完整的錯誤信息嗎? – iamnotmaynard 2014-09-18 18:29:21

+3

刪除您的病例陳述中的「{}」。 – csmckelvey 2014-09-18 18:30:00

+0

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – iamnotmaynard 2014-09-18 18:30:04

回答

1

while是放錯了地方 - 它應該是不switchdo結束後},。請注意,由於它依賴於numberOfProduct變量,因此您需要將其定義移至do-while循環之外。

+0

Thanx eveybody,問題已解決...... – 2014-09-18 18:41:54

+0

@ harpreetkaur-請接受這個答案! – 2014-09-18 18:46:22

相關問題