我试图弄清楚如何格式化数组方法的代码,该方法可以轻松显示Yahtzee游戏的总得分。我一辈子都无法弄清楚如何输入。对于添加内容的任何帮助将不胜感激。把事情简单化。谢谢!
(sumDice是一种方法)
int[] dice = new int[5]; // the array of 5 dice
Scanner scnr= new Scanner(System.in);
Random randGen = new Random(); // new random number generator
public void playGame() {
dice[0] = randGen.nextInt(6) + 1;
dice[1] = randGen.nextInt(6) + 1;
dice[2] = randGen.nextInt(6) + 1;
dice[3] = randGen.nextInt(6) + 1;
dice[4] = randGen.nextInt(6) + 1;
displayDice();
reRoll();
displayDice();
reRoll();
displayDice();
System.out.println("Which value do you want to count?");
int valueToCount = scnr.nextInt();
System.out.println("You have " + countDie(valueToCount) + " dice of value " + valueToCount);
//sum of the dice
System.out.println("The sum of the dice is " + sumDice() );
}
public int countDie(int valueToCount) {
int count = 0;
int i;
for (i=0; i<dice.length; i++) {
if (dice[i] == valueToCount) {
count = count + 1;
}
}
return count;
}
public void displayDice() {
int index; // counter for loop
for (index=0; index<dice.length; index++) {
System.out.print(dice[index] + " ");
}
System.out.println();
}
public void reRoll() {
// re-rolling
String answer;
int i;
for (i=0; i<dice.length; i++ ) {
System.out.println("Re-roll die " + i + " which is a " + dice[i] + " (y/n)?");
answer = scnr.next();
if (answer.equalsIgnoreCase("y")) {
System.out.println("REROLLING THIS DIE!!!");
dice[i] = randGen.nextInt(6) + 1;
}
}
}
}