我正在做一个骰子游戏项目。在游戏中,用户掷出四个骰子,骰子值的总和需要加在一起。roll()方法用于实现这一点。编写滚动方法的说明如下:
“理想情况下,roll()方法应该接受一个参数,说明您想要掷多少个骰子。该方法应该是一个值返回方法,将骰子的值和总和返回给main()。你可以一次掷出所有的骰子,或者一次掷出一个骰子。”
我决定一次掷出四个骰子。在我的老师向我展示的dice程序示例中,有不同的方法返回值,而不仅仅是一个。我的每个方法都返回一个值。然而,当我试图编译它时,我得到了一些错误,每次我返回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;
}
}
感谢所有决定帮忙的人。
问题之所以存在,是因为您在roll方法中声明了 dieValues,因此它们的作用域仅在此方法中。然后你返回实际上不存在的骰值。您需要将 dieValue 创建为类变量,而不是在 roll 方法中使用“this”关键字来设置类变量,而不是“int”类型。请记住将它们声明为静态,因为您的“滚动”方法是静态的
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;
}
}
这里有一些有用的东西。研究一下。理解它。请
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的构造函数。
我建议您的应用程序使用以下方法。
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!");
}
}
}