代码运行但没有生成输出

时间:2017-08-18 02:07:10

标签: java constructor main

努力提高我的编码技巧。运行波纹管代码后,我只收到构建成功消息。所有的帮助都非常感谢提前感谢。

package javacourse;

public class Monopoly {

    public int diceRoll(int sides){
        double randomNum = Math.random() * 6;
        randomNum = randomNum + 1;
        return (int) randomNum;
    }

    public int monopolyRoll(){
        int dice1 = diceRoll(6);
        int dice2 = diceRoll(6);
        int tot = dice1 + dice2;

        if(dice1 == dice2){
            int dice3 = diceRoll(6);
            int dice4 = diceRoll(6);
            tot = tot + dice3 + dice4;
        }

        return tot;
    }

    public static void main(String[] args) {

    }

}

1 个答案:

答案 0 :(得分:0)

代码没有显示任何内容,因为你没有做任何事情,所以在这种情况下你可以使用System.out.println函数执行打印,如下面的示例中所示:

另外,当您学习如何编码时,使用缩进始终很重要,因此您的代码将更容易阅读。

package javacourse;

public class Monopoly {

    public int diceRoll(int sides) {
        double  randomNum = Math.random()*6;
        randomNum =randomNum + 1;
        return (int)randomNum;
    }

    public int monopolyRoll() {
        int dice1 = diceRoll(6);
        int dice2 = diceRoll(6);
        int tot = dice1 + dice2;

        if (dice1 == dice2) {
            int dice3 = diceRoll(6);
            int dice4 = diceRoll(6);
            tot = tot + dice3 + dice4;
        }

        return tot;
    }

    public static void main(String[] args) {
        Monopoly monopoly = new Monopoly();

        System.out.println("monopolyRoll");
        System.out.println(monopoly.monopolyRoll());
    }

}

你不觉得读书更好吗?

另一件事,因为你的函数都是公共的,你需要实例化你的类才能调用它们,因此你必须执行new Monopoly();来创建你的Class的对象。