2016-02-26 12 views
0

下面的代碼已經過測試,所有代碼都運行良好,直到我添加了DecimalFormat。現在它出錯了,並且說它找不到符號「twoDigitPattern」。我試圖使用+ DecimalFormat(平均)),它也是錯誤。DecimalFormat在Java程序中,我得到一個錯誤,說無法找到符號twoDigitPattern

class IntegerInputWithCkAndAverage 
{ 

    public static void main(String[] args) 
    { 
     final int SENTINEL = 0; 
     int numEntered = 0; 
     int accumulator = 0; 
     int counter = 0; 
     double average = 0; 

     Scanner scan = new Scanner(System.in); 
     System.out.print("Enter a integer, or 0 to end > "); 
     while(!scan.hasNextInt()) { 
     String garbage = scan.nextLine(); 
     System.out. 
       print("Invalid input. Please enter an integer, or 0 to end"); 
     } 
     numEntered = Integer.parseInt(scan.nextLine()); 
     while(numEntered != SENTINEL) { 
     accumulator = accumulator + numEntered; 
     counter++; 
     System.out.print("Enter another integer, or 0 to end> "); 
     while(!scan.hasNextInt()) { 
      String garbage = scan.nextLine(); 
      System.out.print( 
        "Invalid input. Please enter an integer or 0 to end"); 
     } 
     numEntered = Integer.parseInt(scan.nextLine()); 
     } 
     average = (double) accumulator/counter; 
     DecimalFormat twoDigitPattern = new DecimalFormat("#0.00"); 
     System.out.println( 
       "the total of the " + counter + "numbers entered is " + 
       accumulator + " and the average is " + twoDigitPattern(average)); 
    } 
} 
+0

做到了我的解決方案的工作? –

回答

1

變化

twoDigitPattern(average) 

twoDigitPattern.format(average) 
+0

是的,這是一個奇怪的錯誤消息,省略了方法解引用。 – markspace

+0

謝謝,我會試試。 – urmagurd

+0

這是完美的。我在網上查看了所有內容,並在我的教科書中看起來很容易,但從字面上看,沒有找到的地方。 – urmagurd

相關問題