首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的代码编译,但没有显示预期的输出。

我的代码编译,但没有显示预期的输出。
EN

Stack Overflow用户
提问于 2017-04-25 03:18:43
回答 2查看 445关注 0票数 1

这是我的编程作业,它成功地编译,但是当我试图运行它时,输出看起来不像它应该做的那样。任何关于我所犯错误的建议都将不胜感激。谢谢

代码语言:javascript
复制
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 #Rand^ rounds / Round在线程"main“中的<=8:d != java.lang.Double at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793) at java.util.Formatter$FormatSpecifier。在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)按任意键继续。。。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-25 03:26:05

看来你得到了一个java.util.IllegalFormatConversionException

对于类型为%f的变量,请确保在printf()中使用的是%d而不是%d,因为%d与Java中的十进制整数一起使用。

改变这一点:

代码语言:javascript
复制
System.out.printf("%10d%10d%15d%12d%15.2f\n", numRound, randomNum, randToRound, modulusOfRand, randDivRound);

对此:

代码语言:javascript
复制
System.out.printf("%10f%10d%15f%12f%15.2f\n", numRound, randomNum, randToRound, modulusOfRand, randDivRound);
票数 1
EN

Stack Overflow用户

发布于 2017-04-25 04:16:06

程序失败的原因是System.out.printf()方法中的格式。您已经将变量randToRound、modulusOfRand定义为double,但您正在尝试通过在格式化程序中使用%10d将它们格式化为int。

请添加System.out.print()以检查变量的值,并在System.out.printf()中添加适当的格式,这将解决您的问题。

可能的解决办法:

代码语言:javascript
复制
System.out.printf("%10d%10d%15d%12d%15.2f\n", (int)numRound, randomNum, (int)randToRound, (int)modulusOfRand,
                    randDivRound);

另外,numRound的循环应该从1开始,循环通过numRound当前逻辑运行9次,这不是问题语句中所期望的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43600998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档