"Throw dice again" Yahtzee 程序的方法
"Throw dice again" method for a Yahtzee program
我正在尝试使用一种方法来随机掷出五个骰子 (int [] dice) 并允许用户说出他们想再次掷出哪个骰子(第 1、2、3、4 或第 5 次死亡)就像在 Yahtzee 游戏中一样。我还必须验证用户是否选择了 1 到 5 之间的数字,他们没有输入重复的数字,并且他们没有输入超过 4 个数字来再次滚动。如果不满足要求,我需要打印"Illegal die!",让用户重新输入。他们选择的任何骰子/骰子都需要再次随机化并分配给 dice[] 数组。到目前为止,这基本上是接受用户字符串输入并将其更改为整数,但我不知道如何做上面提到的其他事情。任何帮助将不胜感激!
public static void throwAgain(int[] dice) {
Scanner keyboard = new Scanner(System.in);
System.out.print("List which die to throw again: ");
String line = keyboard.nextLine();
String [] strArray = line.split(" ");
int [] intArray = new int[strArray.length];
for (int i = 0; i < intArray.length; i++){
intArray[i] = Integer.parseInt(strArray[i]); // Changes user's string input to an array of integers
}
// Arrays.sort(intArray);
}
I also have to validate that the user picked a number between 1 and 5
在循环时检查每个整数。 >
和 <
很有用。
that they don't put a duplicate number
创建一个子循环来检查每个先前的值。 =
很有用。
and that they don't enter more than 4 numbers to roll again.
.length
和 >
很有用。
If the requirements aren't satisfied, I need to out print "Illegal die!"
有一个布尔标志 error
,以 false
开头,并在验证检查失败时将其设置为 true
。最后,检查标志。 System.out.println
很有用。
and ask the user to enter again.
while
很有用。在顶部将 error
设置为 true,在循环开始时将 false
设置为 true。出错时循环
Whichever die/ dice they choose needs to be randomized again and be assigned to the dice[] array
循环 intArray
,并为索引比 intArray
的每个元素小一的 dice
元素分配新的随机值。 []
、-
和 java.util.Random
很有用。
我正在尝试使用一种方法来随机掷出五个骰子 (int [] dice) 并允许用户说出他们想再次掷出哪个骰子(第 1、2、3、4 或第 5 次死亡)就像在 Yahtzee 游戏中一样。我还必须验证用户是否选择了 1 到 5 之间的数字,他们没有输入重复的数字,并且他们没有输入超过 4 个数字来再次滚动。如果不满足要求,我需要打印"Illegal die!",让用户重新输入。他们选择的任何骰子/骰子都需要再次随机化并分配给 dice[] 数组。到目前为止,这基本上是接受用户字符串输入并将其更改为整数,但我不知道如何做上面提到的其他事情。任何帮助将不胜感激!
public static void throwAgain(int[] dice) {
Scanner keyboard = new Scanner(System.in);
System.out.print("List which die to throw again: ");
String line = keyboard.nextLine();
String [] strArray = line.split(" ");
int [] intArray = new int[strArray.length];
for (int i = 0; i < intArray.length; i++){
intArray[i] = Integer.parseInt(strArray[i]); // Changes user's string input to an array of integers
}
// Arrays.sort(intArray);
}
I also have to validate that the user picked a number between 1 and 5
在循环时检查每个整数。 >
和 <
很有用。
that they don't put a duplicate number
创建一个子循环来检查每个先前的值。 =
很有用。
and that they don't enter more than 4 numbers to roll again.
.length
和 >
很有用。
If the requirements aren't satisfied, I need to out print "Illegal die!"
有一个布尔标志 error
,以 false
开头,并在验证检查失败时将其设置为 true
。最后,检查标志。 System.out.println
很有用。
and ask the user to enter again.
while
很有用。在顶部将 error
设置为 true,在循环开始时将 false
设置为 true。出错时循环
Whichever die/ dice they choose needs to be randomized again and be assigned to the dice[] array
循环 intArray
,并为索引比 intArray
的每个元素小一的 dice
元素分配新的随机值。 []
、-
和 java.util.Random
很有用。