首页 > 解决方案 > Java骰子游戏项目:将骰子的值返回给main方法

问题描述

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

“理想情况下,roll() 方法应该接受一个参数,说明你想掷多少个骰子。该方法应该是一个返回值的方法,它返回骰子/骰子的值以及 main() 的总和。你可以一次掷出所有的骰子,或者一次掷一个骰子。”

我决定一次掷四个骰子。在我的老师向我展示的示例骰子程序中,有不同的方法可以返回值,而不仅仅是一个。我的每个方法都返回一个值。但是,当我尝试编译它时,每次返回 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

解决方案


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

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;
    }
}

推荐阅读