2016-12-15 59 views
-4

我被告知最後我的格式有問題。但是,我無法弄清楚會是什麼...錯誤信息在代碼的最後。 請不要對你的回答產生懷疑,並告訴我關注或回顧的內容(如格式)。我需要具體的事情來改變。謝謝!! 下面的代碼:我的Java代碼中的格式有什麼問題?

public static void main(String[] args) 
{ 
    double pennies = 0.01;  //number of pennies 
    double totalPennies = 0.01; //the variable that is added to 
    int maxDays;   //number of days the loop will double the pennies to 
    int day = 0;    //the variable used to count each day in the loop 

    //Create a String variable for user input with a console window. 
    String input; 
    //Create a scanner object for keyboard input into the console. 
    Scanner keyboard = new Scanner(System.in); 

    //Ask user for the number of days to count. 
    input = JOptionPane.showInputDialog("Enter the maximum number of days worked in your pay period" 
      + " and I will tell you how much money you made. Please enter at least 1."); 
    maxDays = Integer.parseInt(input);  //this makes the user's string input into 
             //an integer data type 

    //Input Validation 
    while(maxDays < 1) 
    { 
     System.out.println("You have entered a number that " 
       + "is less than 1. \nPlease enter a number that is at" 
       + " least 1 or more:"); 
     maxDays = keyboard.nextInt(); 
    } 


    //next we have the first loop that will only execute if the user enters at least 1 
    while(maxDays >= 1) 
    { 
     //here's the chart 
     System.out.println("Day  Day's Income Total "); //titles for the chart 
     System.out.println("----------------------------------"); //34 dashes/spaces 
     System.out.println("1   $0.01    $0.01 "); //the first day 

     //this next nested loop will do the actual counting and add to the chart with 
     //each iteration 
     for(day = 2; day <= maxDays; day++) 
     { 
      pennies = pennies * 2;  //double pennies 
      totalPennies += pennies; //add the new value of pennies to the running total 

      //add information from each iteration to the chart 
      System.out.printf("%10.0d" + "$%18.0f" + "$%6.0f", day, pennies, totalPennies); 


     } 

    } 
} 

下面是錯誤信息:

Exception in thread "main" java.util.IllegalFormatPrecisionException: 0 
at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2984) 
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2729) 
at java.util.Formatter.parse(Formatter.java:2560) 
at java.util.Formatter.format(Formatter.java:2501) 
at java.io.PrintStream.format(PrintStream.java:970) 
at java.io.PrintStream.printf(PrintStream.java:871) 
at assignment4PenniesForPay.assignment4PenniesForPay.main(assignment4PenniesForPay.java:65) 

65號線是格式化線。

+3

關於''「請不要對你的答案有任何含糊之處,並告訴我關注或回顧的內容(如格式化)。我需要具體的事情來改變。」' - 請不要SHOUT或要求苛刻。如果你仍然保持平靜,你會得到更好的答案。 –

+0

'$%18.0f「'?爲什麼要用18位前數字? –

+1

現在在這裏稍等一下,爲什麼在使用'for'循環時有'while'循環?改變:while(maxdays> = 1){如果(maxDays> = 1){否則你的循環將無限期地運行。 – DevilsHnd

回答

0

%10.0d期待一個整數,但你在那裏增加一個小數位。