Java 2()等等
即使在其他方法中再次定义相同的变量,它们也不会共享任何内容,它们将被视为完全不同的变量。这也是为什么会出现“找不到符号”错误的原因,因为Java 2()等等,java,Java,即使在其他方法中再次定义相同的变量,它们也不会共享任何内容,它们将被视为完全不同的变量。这也是为什么会出现“找不到符号”错误的原因,因为roll_1()中没有可用的dieValue1,以此类推 如果要将某些内容传递回调用方法(如roll())的代码,则必须使用return语句,例如: public int roll() { // Create a Random class object. Random dieRoll = new Random(); // Get a
roll_1()
中没有可用的dieValue1
,以此类推
如果要将某些内容传递回调用方法(如roll()
)的代码,则必须使用return
语句,例如:
public int 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;
}
这也正是说明书中所描述的:
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);
该方法应该是返回值的方法,该方法返回骰子/骰子的值
现在,练习的下一部分是告诉roll()
方法希望滚动模具多少次。为此,您必须传递一个参数,如说明中所述:
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);
理想情况下,roll()
要做到这一点,您必须更改您的方法,如下所示:
public int 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次):
请注意,main()
方法中的缩进有点混乱。如果不使用大括号({}
)包围If
语句的主体,则只执行一个操作。在本例中,这将是System.out.println(“您赢了!”)
。无论diceSum
是什么,都会执行System.exit(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!");
}
}
}
package com.example.demo;
公共类myclass{
整数和=0;
公共虚空游戏(int numberofdice,int numberofeyes){
对于(inti=1;i我建议您使用以下方法
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!");
}
}
}
package com.example.demo;
公共类myclass{
整数和=0;
公共虚空游戏(int numberofdice,int numberofeyes){
对于(int i=1;i)我们还没有在课堂上学习数组,所以我的老师不希望我们在程序中使用数组。但是,是的,我会在下一个活动中记住Dieroll类(这需要为骰子制作一个类).我们还没有在课堂上学习数组,所以我的老师不希望我们在程序中使用数组。但是,是的,我会记住下一个活动中的DieRoller类(这需要为骰子制作一个类).谢谢!我想这就是我老师对这个项目的要求,我会检查。谢谢你也发现了我的错误。谢谢你!我想这就是我老师对这个项目的要求,我会检查。谢谢你也发现了我的错误。谢谢你!我肯定会使用for循环!谢谢!我肯定会使用for-loop!谢谢你指出关于范围的错误,这是一个诚实的错误。这看起来会起作用,我会试试。谢谢你指出关于范围的错误,这是一个诚实的错误。这看起来会起作用,我会试试。
public int 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;
}
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);
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!");
}
}
}