在这段代码我写在这里的用户输入他们是否喜欢选择的头或者尾巴在coinflip游戏。 我想保持帐簿的如何许多次首脑会出现面或反面显示出它每次变化。 经过数小时的艰难和寻找我不找我们的完美所以如果有人可以让我知道什么我可以利用让我知道。
import java.util.Random;
import java.util.Scanner;
public class CoinToss {
private enum Coin {
Head, Tail
}
public static void main(String[] args) {
CoinToss game = new CoinToss();
game.startGame();
}
private void startGame() {
Scanner scanner = new Scanner(System.in);
Coin guess;
while (true) {
System.out.print("Enter your guess whether the coin will be heads or tails. Type 1 for heads, 2 for tails, or 3 to quit: ");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("3")) {
break;
} else if (choice.equalsIgnoreCase("1")) {
guess = Coin.Head;
} else if (choice.equalsIgnoreCase("2")) {
guess = Coin.Tail;
} else {
System.out.println("Please select either heads tails or quit.");
continue;
}
Coin toss = tosscoin();
if (guess == toss) {
System.out.println("You guessed correctly!");
} else {
System.out.println("You guessed incorrectly");
}
}
scanner.close();
}
private Coin tosscoin() {
Random r = new Random();
int sideup = r.nextInt(2);
if (sideup == 1) {
return Coin.Head;
} else {
return Coin.Tail;
}
}
}