2011-09-24 82 views
1

我正在學習書籍「Java,如何編程」練習。我應該編寫一個程序,以英里爲單位驅動用戶類型,並使用公升汽油用於未確定的行程次數。錯誤消息:「標識符預計」

該程序應該打印每英里每公升的旅程,並在最後平均英里公升。

我應該使用定點控制的重複,並打印英里公升作爲浮點值。

總是收到的錯誤消息時,我試圖編譯:

GasolineUsage.java:39:預期 milPrLiter =(雙)totalMil/totalLiters; (「The car drives%.2f miles pr liter gasoline \ n \ n」,milPrLiter);以及其中,
^ GasolineUsage.java:40: 類型System.out.printf的非法啓動 ( 「汽車驅動%.2f英里PR升 汽油\ n \ n」 個,milPrLiter);
^ GasolineUsage.java:40: expected System.out.printf(「The car drives%.2f miles pr liter gasoline \ n \ n」,milPrLiter);

^GasolineUsage.java:42:類,接口,或枚舉預期}^5個 錯誤

的代碼如下:

import java.util.Scanner; 
public class GasolineUsage 
{ 

    public static void main(String[] args) 
    {  
     int totalMil = 0; 
     int totalLiters = 0; 
     double milPrLiter1 = 0; 
     double milPrLiter; 

     Scanner inputMil = new Scanner(System.in); 
     Scanner inputLiters = new Scanner(System.in); 

     System.out.print("Type in miles first trip, to end type: -1 "); 
      int mil = inputMil.nextInt(); 
     System.out.print("Type in liters of gasoline used on first trip "); 
      int liters = inputLiters.nextInt(); 
      totalMil = totalMil + mil; 
      totalLiters = totalLiters + liters; 
     milPrLiter1 = (double) mil/liters; 
     System.out.printf("(%.2f miles pr liter this trip.)\n", milPrLiter1); 

     System.out.print("Type in miles on next trip "); 
      while (mil != -1) 
       { 
       mil = inputMil.nextInt(); 
        if (mil != -1) 
         {totalMil = totalMil+ mil; 

         System.out.print("Type in lters used on next trip: "); 
         liters = inputLiters.nextInt();} 
         totalLiters = totalLiters + liters; 

         milPrLiter1 = (double) mil/liters; 
         System.out.printf("(%.2f miles pr liter this trip.)\n", milPrLiter1);} 
       } 

       milPrLiter = (double) totalMil/totalLiters; 
       System.out.printf("The car drives %.2f miles pr liter gasoline\n\n", milPrLiter);    
    } 
} 

任何人都可以幫我嗎?

回答

1

計算你的開合花括號,因爲它們應該匹配。隱藏在代碼中的額外左大括號會在您搜索時發現。請注意,對於這種類型的錯誤,查看拋出錯誤的行上方總是很好。