2017-04-25 21 views
1

これは私のプログラミング宿題です。コンパイルは成功しましたが、実行しようとすると出力がどうなるかのように見えません。私が作った間違いの提案は、大いに感謝するでしょう。 最大乱数が< = 8 はあなたが6 ラウンドランド#を実行したいラウンド数を入力する必要がある9 を使用することが最大の乱数を入力してください。おかげで私のコードはコンパイルされますが、期待される出力は表示されません

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 

は、これは私が見ていますものですRand ^ラウンドモジュラスランド/ラウンド スレッド "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.java:2770) at java.util.Formatter.format(Formatter.java:2520) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream。 java:871) at FRUnit5.main(FRUnit5.java:76) 任意のキーを押して続行します。 。 。

+2

あなたの質問にもっと力を入れ、さらに関連性のある有用な情報を伝えることを強く検討してください。例えば、あなたはどんなアウトプットを期待していますか?何を見ていますか? –

+1

また、 ''それは実行されません」と述べていますが、後で '' ...実行しようとすると、出力がどのようになっているかのようには見えません。プログラム**が実際に実行されていることを示唆していますが、あなたが見たいと思っているものは表示されません。再度、明確にしてください。 –

+0

プログラムは実行されますが、私が期待していた書式化された "printf"の変数は表示されません。これは私が見ているものです: –

答えて

1

java.util.IllegalFormatConversionExceptionが表示されているようです。

%dは、Javaでの10進整数で行くので、あなたは%f代わりの種類doubleの変数のためのあなたのprintf()%dを使用してください。これ

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回実行されます。

関連する問題