Java骰子游戏项目:将骰子的值返回给main方法

问题描述 投票:1回答:4

我正在做一个骰子游戏项目。在游戏中,用户掷四个骰子,并且需要将骰子的值的总和加在一起。 roll()方法用于执行此操作。编写roll方法的说明如下:

“理想情况下,roll()方法应该采用一个参数来说明你想要滚动多少骰子。该方法应该是一个返回值的方法,它返回die / dice的值和sum()的总和。你可以立刻滚动所有骰子,或者一次骰子。“

我决定一次掷四个骰子。在我的老师向我展示的示例骰子程序中,有不同的方法返回值而不是一个。我的每个方法都返回一个值。但是,当我尝试编译它时,每次返回dieValue时都会出现一些错误,上面写着“找不到符号”;虽然我不确定为什么,因为我已经查看了这个并在标题中添加了“int”并在程序的其他区域多次声明变量并仍然得到错误。我也尝试将这个编译成较少的方法,就像我老师建议的那样,但是当我试图弄清楚如何制作一个说明你想要滚动多少骰子的论据时我陷入了困境。所以我坚持我目前的方法,但我仍然不确定什么是错的。

这是我的代码部分处理这个:

        // Roll the dice (redirect to the roll() methods) and declare variables to hold the values that have returned. 
        int dieValue1 = roll_1();
        int dieValue2 = roll_2();
        int dieValue3 = roll_3();
        int dieValue4 = roll_4();

        // Declare a variable for the sum of the dice values (and add the dice values to get that sum).
        int diceSum = dieValue1 + dieValue2 + dieValue3 + dieValue4;

        // Print the sum of the rolled dice. 
        System.out.println("Your sum of the dice values: ");
        System.out.println(diceSum);

        // Determine if the user won or not. 
        if (diceSum == 6 ||diceSum == 12 ||diceSum == 13 ||diceSum == 17 || diceSum == 19 ||diceSum == 23)
            System.out.println("You win!");
            System.exit(0);

    }
    // roll() method header that rolls the die (creates a Die object and gets a random value for the object).
        public void roll()
        {
            // Create a Random class object. 
            Random dieRoll = new Random();

            // Get a random integer value for the dice between 1 and 6.
            int dieValue1 = dieRoll.nextInt(6) + 1;
            int dieValue2 = dieRoll.nextInt(6) + 1;
            int dieValue3 = dieRoll.nextInt(6) + 1;
            int dieValue4 = dieRoll.nextInt(6) + 1;
        }

    // method that returns the value of die1.
        public static roll_1()
        {
            return dieValue1;
        }
    // method that returns the value of die2.
        public static int roll_2()
        {
            return dieValue2;
        }
    // method that returns the value of die3.
        public int roll_3()
        {
            return static dieValue3;
        }
    // method that returns the value of die4.
        public static int roll_4()
        {
            return dieValue4;
        }
    // method that returns the sum of the values of die1, die2, die3, and die4. 
        public static int sum()
        {
            return dieValue1 + dieValue2 + dieValue3 + dieValue4;
        }
}

感谢任何决定提供帮助的人。

java
4个回答
0
投票

问题的存在是因为您在roll方法中声明了dieValues,因此它们的范围仅在此方法中。然后你返回实际上不存在的dieValue。您需要创建dieValue作为类变量,而不是滚动方法中的“int”类型使用“this”关键字来设置类变量。并记住将它们声明为静态,因为您的“roll”方法是静态的

import java.util.Random;

public class Roll {
    static int dieValue1;
    static int dieValue2;
    static int dieValue3;
    static int dieValue4;

    public static void main(String[] args) {
        roll();
        int dieValue1 = roll_1();
        int dieValue2 = roll_2();
        int dieValue3 = roll_3();
        int dieValue4 = roll_4();

        // Declare a variable for the sum of the dice values (and add the dice values to
        // get that sum).
        int diceSum = dieValue1 + dieValue2 + dieValue3 + dieValue4;

        // Print the sum of the rolled dice.
        System.out.println("Your sum of the dice values: ");
        System.out.println(diceSum);

        // Determine if the user won or not.
        if (diceSum == 6 || diceSum == 12 || diceSum == 13 || diceSum == 17 || diceSum == 19 || diceSum == 23)
            System.out.println("You win!");
        System.exit(0);

    }

// roll() method header that rolls the die (creates a Die object and gets a random value for the object).
    public static void roll() {
        // Create a Random class object.
        Random dieRoll = new Random();

        // Get a random integer value for the dice between 1 and 6.
        dieValue1 = dieRoll.nextInt(6) + 1;
        dieValue2 = dieRoll.nextInt(6) + 1;
        dieValue3 = dieRoll.nextInt(6) + 1;
        dieValue4 = dieRoll.nextInt(6) + 1;
    }

// method that returns the value of die1.
    public static int roll_1()
    {
        return dieValue1;
    }

// method that returns the value of die2.
    public static int roll_2() {
        return dieValue2;
    }

// method that returns the value of die3.
    public static int roll_3()
    {
        return dieValue3;
    }

// method that returns the value of die4.
    public static int roll_4() {
        return dieValue4;
    }

// method that returns the sum of the values of die1, die2, die3, and die4. 
    public static int sum() {
        return dieValue1 + dieValue2 + dieValue3 + dieValue4;
    }
}

这不是最佳解决方案,但我尝试以最少的更改运行您的程序。我建议不要使用静态因为它在这里没有意义。

以下是我的问题解决方案:

public class Roll {
    public static void main(String[] args) {

        int diceSum = roll();

        // Print the sum of the rolled dice.
        System.out.println("Your sum of the dice values: ");
        System.out.println(diceSum);

        // Determine if the user won or not.
        if (diceSum == 6 || diceSum == 12 || diceSum == 13 || diceSum == 17 || diceSum == 19 || diceSum == 23)
            System.out.println("You win!");
        System.exit(0);

    }

    public static int roll() {
        Random dieRoll = new Random();
        int diceSum = 0;

        diceSum += dieRoll.nextInt(6) + 1;
        diceSum += dieRoll.nextInt(6) + 1;
        diceSum += dieRoll.nextInt(6) + 1;
        diceSum += dieRoll.nextInt(6) + 1;
        return diceSum;
    }
}

0
投票

这是有用的东西。研究它。明白它。请。

package com.inlet.ifserver;

import java.util.Random;

public class DieRoller {

    private Random dieRoll;

    private int dieValue1;
    private int dieValue2;
    private int dieValue3;
    private int dieValue4;

    // roll() method header that rolls the die (creates a Die object and gets a random value for the object).
    public DieRoller()
    {
        Random dieRoll = new Random();
        dieRoll.setSeed(System.currentTimeMillis());
    }

    public void roll() {

        // Get a random integer value for the dice between 1 and 6.
        dieValue1 = dieRoll.nextInt(6) + 1;
        dieValue2 = dieRoll.nextInt(6) + 1;
        dieValue3 = dieRoll.nextInt(6) + 1;
        dieValue4 = dieRoll.nextInt(6) + 1;

    }
    // method that returns the value of die1.
    public int roll_1()
    {
        return dieValue1;
    }
    // method that returns the value of die2.
    public int roll_2()
    {
        return dieValue2;
    }
    // method that returns the value of die3.
    public int roll_3()
    {
        return dieValue3;
    }
    // method that returns the value of die4.
    public int roll_4()
    {
        return dieValue4;
    }
    // method that returns the sum of the values of die1, die2, die3, and die4.
    public int sum()
    {
        return dieValue1 + dieValue2 + dieValue3 + dieValue4;
    }

    public static void main (String [] args) {

        DieRoller roller = new DieRoller();
        roller.roll();

        // Roll the dice (redirect to the roll() methods) and declare variables to hold the values that have returned.
        int dieValue1 = roller.roll_1();
        int dieValue2 = roller.roll_2();
        int dieValue3 = roller.roll_3();
        int dieValue4 = roller.roll_4();

        // Declare a variable for the sum of the dice values (and add the dice values to get that sum).
        int diceSum = roller.sum();

        // Print the sum of the rolled dice.
        System.out.println("Your sum of the dice values: ");
        System.out.println(diceSum);

        // Determine if the user won or not.
        if (diceSum == 6 ||diceSum == 12 ||diceSum == 13 ||diceSum == 17 || diceSum == 19 ||diceSum == 23)
            System.out.println("You win!");
    }
}

你能解释一下这里发生的一切吗?也许你可以把它变成一般。而不是4个骰子,N骰子。您可以使用骰子值数组“private int [] dieValues”而不是单个骰子变量。将骰子的数量作为参数传递给DieRoller的构造函数。


0
投票

roll()方法中,您定义了4个整数字段,dieValue1dieValue2等。这些变量在方法本身内声明,因此不能在任何其他方法中“读取”,例如roll_1()roll_2()等。

即使您在其他方法中再次定义相同的变量,它们也不共享任何内容,它们将被视为完全不同的变量。这也是为什么你得到那些“找不到符号”的错误,因为dieValue1中没有roll_1()等等。

如果要将某些内容传递回调用roll()等方法的代码,则必须使用return语句,例如:

public void roll() {
    // Create a Random class object. 
    Random dieRoll = new Random();

    // Get a random integer value for the dice between 1 and 6.
    return dieRoll.nextInt(6) + 1;
}

这也正是指示中描述的内容:

该方法应该是一个返回值的方法,它返回die / dice的值

现在,练习的下一部分是告诉roll()方法你想要掷骰子的次数。要做到这一点,你必须传递一个参数,如说明中所述:

理想情况下,roll()方法应该采用一个论证来说明你想要掷多少骰子。

要做到这一点,你必须改变你的方法,如下所示:

public void roll(int times) {
    // Create a Random class object. 
    Random dieRoll = new Random();

    // Get a random integer value for the dice between 1 and 6.
    return dieRoll.nextInt(6) + 1;
}

这也意味着roll_1()roll_2()等方法都已过时,因为你必须将次数作为参数传递,而不是每次都声明一个新方法。您现在可以删除这些方法。

仅凭这一点是不够的,因为你仍然只在你的roll()方法中滚动一次。要解决这个问题,您可以使用循环结构,重复相同的操作,直到达到某个阈值(在这种情况下,直到达到n次):

public void roll(int times) {
    // Create a Random class object. 
    Random dieRoll = new Random();

    int dieSum = 0;
    for (int time = 1; time <= times; time++) {
        // Get a random integer value for the dice between 1 and 6.
        int dieValue = dieRoll.nextInt(6) + 1;
        dieSum = dieSum + dieValue;
    }
    return dieSum;
}

在这种情况下,我们使用了for循环,声明了一个变量time,它将每个值增加一个值,直到它不再小于或等于times

这也意味着你不需要sum()方法,因为roll()方法现在已经为你计算了总和。您也可以删除此方法。

最后一部分是在你的main()方法中实际调用方法,如指示所述:

int diceSum = roll(4);
if (diceSum == 6 ||diceSum == 12 ||diceSum == 13 ||diceSum == 17 || diceSum == 19 ||diceSum == 23)
    System.out.println("You win!");
System.exit(0);

请注意,您在main()方法中的缩进有点令人困惑。如果不使用花括号({})来包围if语句的主体,则只执行一个操作。在这种情况下,这将是System.out.println("You win!")。无论System.exit(0)是什么,都执行diceSum操作。


0
投票

我建议您的应用程序使用以下方法。

package com.example.demo;

public class myclass {

    int sum = 0;

    public void play(int numberofdices, int numberofeyes) {
        for (int i = 1; i <= numberofdices; i++) {
            sum = sum + (int) ((Math.random()) * numberofeyes + 1);
        }

        // Print the sum of the rolled dice.
        System.out.println("Your sum of the dice values: ");
        System.out.println(sum);

        // Determine if the user won or not.
        if (sum == 6 || sum == 12 || sum == 13 || sum == 17 || sum == 19 || sum == 23) {
            System.out.println("You win!");
        } else {
            System.out.println("You loose!");
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.