2017-04-25 66 views
1

這是我編寫的作業分配,它成功編譯,但是當我嘗試運行它時,輸出看起來不像它應該如何。任何關於我犯過的錯誤的建議都將非常感激。由於我的代碼編譯,但沒有顯示預期的輸出

import java.util.Scanner; 
/*********************************** 
* Class: FRUnit5.java 
* Author: Robert Frankele 
* 
* Description: Ask user to enter the maximum random number of rounds (2-8). 
* Then ask the user how many rounds they would like to execute (1-8). 
* Display the results in formatted columns as follows: 
* (example using 8 as max random number, and 2 and number of rounds) 
* 
* Round Rand #  Rand^Round  Modulus  Rand/Round 
*  1   7    7   0   7.00 
*  2   4    16   0   2.00 
*/ 
public class FRUnit5 
{ 

    public static void main(String[] args) 
    { 
     // Create a Scanner object for user input: 2pts 
     Scanner userInput = new Scanner(System.in); 
     // Variable to hold user's input for maximum random number: 2pts 
     double maxRandNum; 
     // Variable to hold the number of rounds the user wishes to execute: 2pts 
     double numRound; 
     double maxRound; 

     // Variable to hold the random number generated from the Math class 
     // using the user's maximum random number as an argument: 2pts 
     double randNumMath; 
     // Ask the user to enter the maximum random number to use (2-8): 2pts 
     System.out.print("Enter the maximum random number to use "); 
     // Store the number they entered into the variable declared above: 2pts 
     maxRandNum = userInput.nextInt(); 
     // Clear the buffer 
     userInput.nextLine(); 

     // Check to see if the number they entered is greater than 8: 5pts 
     // If it is, display an error message and assign 8 as the maximum random number (see project example): 3pts 
     // Then, assign 8 to the variable declared above that will hold the maximum random number: 3pts 
      if (maxRandNum > 8) 
     { 
      System.out.println("The maximum random number has to be <= 8"); 
      maxRandNum = 8; 
     } 

     // Ask the user to enter the number of rounds they wish to execute.: 2pts 
     System.out.print("Enter the number of rounds you wish to execute "); 
     // Store the number they entered into the variable declared above: 2pts 
     numRound = userInput.nextInt(); 

     // Print the header of the output, using the printf method: 8pts 
     // Round Rand #  Rand^Round  Modulus  Rand/Round 
     System.out.printf("%10s%10s%15s%12s%15s\n", "Round", "Rand #", "Rand^Round", "Modulus", "Rand/Round"); 

     // Set up a for loop to iterate through the number of rounds: 10pts 

     maxRound = 9; 
     for (numRound = 0; numRound < maxRound; numRound++) 
     { 
      // Calculate the random number given the maximum random number, and store in a local variable called randomNum: 5pts 
      int randomNum = (int) (Math.random() * maxRandNum + 1); 


      // Calculate the Rand^Round number, using Math.pow(randomNum, round) as the equation, 
      // and store in a local variable called randToRound: 5pts 
      double randToRound = (Math.pow(randomNum, numRound)); 

      // Calculate the modulus: randomNum % round, and store in a variable called modulusOfRand: 3pts 
      double modulusOfRand = randomNum % numRound; 

      // Calculate randomNum/round and store in a variable called randDivRound: 3pts 
      double randDivRound = randomNum/numRound; 

      // Using printf, display the results, remembering to only display randDivRound with two places after the decimal point: 10pts 
      System.out.printf("%10d%10d%15d%12d%15.2f\n", numRound, randomNum, randToRound, modulusOfRand, randDivRound); 

     } 

    } // end of main 
} // end of class 

這是我所看到的: 輸入最大隨機數使用9 的最大隨機數必須是< = 8 輸入要執行6 回合蘭德#輪數Rand^Round Modulus Rand/Round 線程「main」中的異常java.util.IllegalFormatConversionException:d!= java.lang.Double at java.util.Formatter $ FormatSpecifier.failConversion(Formatter.java:4302) at java。 util.Formatter $ FormatSpecifier.printInteger(Formatter.java:2793) at java.util.Formatter $ FormatSpecifier.print(Fo (PrintStream.format(PrintStream.java:970) )。在java.io.PrintStream.printf(PrintStream.printf.Replication.format(Formatter.java:2520) (java.io.PrintStream.format(PrintStream.java:970) ) java:871) at FRUnit5.main(FRUnit5.java:76) 按任意鍵繼續。 。 。

+2

請強烈考慮在您的問題中投入更多努力,並告訴我們更多相關和有用的信息。例如 - 你期望輸出什麼?你在看什麼? –

+1

另外,你聲明''它不會運行'' - 但後來聲明''...當我嘗試運行它時,輸出看起來不像應該如何運行。「 - 這表明程序**實際上運行了,但是並沒有向您顯示您期望看到的內容。請再次澄清。 –

+0

該程序確實運行,但不顯示我期待的格式化「printf」中的變量。這是我所看到的: –

回答

1

看來你得到了java.util.IllegalFormatConversionException

確保您在printf()使用%f代替%ddouble類型的變量,因爲%d去與Java中的一個十進制整數。

變化這樣的:

System.out.printf("%10d%10d%15d%12d%15.2f\n", numRound, randomNum, randToRound, modulusOfRand, randDivRound); 

這樣:

System.out.printf("%10f%10d%15f%12f%15.2f\n", numRound, randomNum, randToRound, modulusOfRand, randDivRound); 
+1

謝謝,你是對的,這是問題。我感謝您的幫助!當你編寫所有的代碼時,這是令人沮喪的,然後最後一行是錯誤。 –

+0

很高興幫助。如果解決方案已解決您的問題,請不要忘記標記爲已接受^^。 –

0

的原因程序的失敗是在System.out.printf()方法的格式。您已經將變量randToRound,modulusOfRand定義爲double,但您試圖使用格式化程序中的%10d將它們格式化爲int。

請添加System.out.print()來檢查變量的值並在System.out.printf()中添加適當的格式,這應該可以解決您的問題。

可能的解決方案:

System.out.printf("%10d%10d%15d%12d%15.2f\n", (int)numRound, randomNum, (int)randToRound, (int)modulusOfRand, 
        randDivRound); 

而且你對numRound循環應該從1開始,並遍歷當前numRound邏輯將運行環路來說並不算什麼問題陳述expeccted 9倍。