不兼容的类型必须为int,但发现为空

时间:2018-10-30 15:25:05

标签: java javahelp

我正在尝试将骰子放入代码中,但是当我放入骰子并尝试掷入它时,它表示类型不兼容。

package domain;

public class Dice {
    /**
     * creating integers for 2 dice and sum of these
     */
    private int dice1, dice2, sum;

    /**
     * Gets the number of die1
     */
    public int getDie1() {
        return dice1;
    }

    /**
     * Gets number of die2
     */
    public int getDie2() {
        return dice2;
    }

    /**
     * Gets the sum of both dice
     */
    public int getSum() {
        return sum;
    }

    /**
     * Rolls the dice and gets sum for both dice
     */
    public void rollDice() {
        dice1 = rollDice();
        dice2 = rollDice();
        sum = (dice1 + dice2);
    }

    /**
     *
     * @return
     * Makes the dice roll randomly from 1-6
     */
    private int rollDie() {return (int) ((Math.random()*6));}

3 个答案:

答案 0 :(得分:1)

您使用了错误的方法。您正在使用rollDice()而不是rollDie()

将其更改为此:

public void rollDice() {
    dice1 = rollDie();
    dice2 = rollDie();
    sum = (dice1 + dice2);
}

答案 1 :(得分:1)

我认为您在调用错误的方法。使用rollDie()代替rollDice()

 /**
     * Rolls the dice and gets sum for both dice
     */
    public void rollDice() {
        dice1 = rollDie();
        dice2 = rollDie();
        sum = (dice1 + dice2);
    }

答案 2 :(得分:0)

rollDice()函数似乎具有空返回类型,您正在将其分配给整数dice1,dice2和sum的值。