我制作了一个程序,可以与 2 个用户一起玩游戏。他们选择一个起始号码,每回合可以选择 1、2 或 3 个号码。谁先达到0,谁输。我的代码可能会让游戏更清晰一点:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int rock = 0, player1 = 0, player2 = 0, menu = 0, win1 = 0, win2 = 0, turns1 = 0, turns2 = 0, rocks1 = 0, rocks2 = 0;
double sqrt = 0;
String name1 = "", name2 = "";
boolean bool = true;
// Prompts users with names
System.out.println("Welcome to the game, Get Rocked!");
System.out.println("What is Player 1's name?");
name1 = s.nextLine();
System.out.println("What is Player 2's name?");
name2 = s.nextLine();
System.out.printf("\nHello, %s and %s!", name1, name2); // 2 players
while (bool == true) {
if (menu == 0) { // displays a menu and interface
System.out.println("\nWelcome to the menu. Please choose an option:");
System.out.println("1. How to play \n2. Play game \n3. Exit");
menu = s.nextInt();
}
if (menu == 1) {
System.out.println("\nIn the game, Get Rocked, there is a starting number of rocks.");
System.out.println("Each player takes between 1 and 3 rocks, every turn.");
System.out.println("The player that takes the last rock loses, or... GETS ROCKED!");
while (menu != 0) { // user returns to main menu after "How to Play" is displayed
System.out.println("Enter 0 to continue");
menu = s.nextInt();
}
}
else if (menu == 2) {
// user must choose a rock starting number >= 16 AND a perfect square
// checks if rock is less than 16 and square root value is not equal to original rock number
while (rock < 16 || ((sqrt * sqrt) != rock)) { // if it is, then it prompts user again
System.out.println("\nPlease enter a starting rock number, that is greater than or equal to 16, AND a perfect square:");
rock = s.nextInt();
sqrt = Math.floor(Math.sqrt(rock)); // gets the square root value of the rock number, rounded down
}
System.out.println("There are " + rock + " rocks");
while (rock != 0) { // loop to complete game
while ((player1 < 1 || player1 > 3) && rock != 0) { // loops code until player chooses between 1 and 3 rocks
System.out.println(name1 + ", how many rocks do you want to take?"); // prompts users with names
player1 = s.nextInt();
if (player1 > 0 && player1 < 4) { // players can only take 1 to 3 rocks
if (player1 > rock) { // players can not take more rocks than there are left
System.out.println("There are only " + rock + " rocks left");
player1 = 0; // resets guess value so that above loop can run
}
else {
rock -= player1;
System.out.println("There are now " + rock + " rocks left"); // announces number of rocks left
turns1++; // counts total turns
rocks1 += player1; // counts total rocks picked
if (rock == 0) { // player loses if they pick up last rock
System.out.println(name1 + " GOT ROCKED!!"); // prompts users with names
win2++; // counts total wins
}
}
} else { // makes sure user enters a proper rock value
System.out.println("Please enter a value from 1 to 3");
}
}
while ((player2 < 1 || player2 > 3) && rock != 0) { // loops code until player chooses between 1 and 3 rocks
System.out.println(name2 + ", how many rocks do you want to take?"); // prompts users with names
player2 = s.nextInt();
if (player2 > 0 && player2 < 4) { // players can only take 1 to 3 rocks
if (player2 > rock) { // players can not take more rocks than there are left
System.out.println("There are only " + rock + " rocks left");
player2 = 0; // resets guess value so that above loop can run
}
else {
rock -= player2;
System.out.println("There are now " + rock + " rocks left"); // announces number of rocks left
turns2++; // counts total turns
rocks2 += player2; // counts total rocks picked
if (rock == 0) { // player loses if they pick up last rock
System.out.println(name2 + " GOT ROCKED!!"); // prompts users with names
win1++; // counts total wins
}
}
} else { // makes sure user enters a proper rock value
System.out.println("Please enter a value from 1 to 3");
}
}
// resets player values for next round
player1 = 0;
player2 = 0;
}
menu = 4;
while (menu == 4) {
// asks user to play again
String answer = ""; // local variable, no need for global variable
System.out.println("\nWould you like to play again? y/n");
answer = s.next();
if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y"))
menu = 2; // restarts game if user chooses to play again
else if (answer.equalsIgnoreCase("no") || answer.equalsIgnoreCase("n"))
menu = 0; // goes back to main menu if user chooses not to play again
else
System.out.println("Please enter a valid answer.");
}
}
else if (menu == 3) {
if (win1 > 0 || win2 > 0) { // shows the scoreboard (wins, turns taken, rocks picked, etc.)
System.out.println("\nHere is the scoreboard!");
System.out.printf("%-20s %s%n", name1, name2);
System.out.printf("%d total wins \t\t %d total wins%n", win1, win2);
System.out.printf("%d total turns \t\t %d total turns%n", turns1, turns2);
System.out.printf("%d rocks picked \t %d rocks picked%n", rocks1, rocks2);
}
// prompts users with names
// program completes when user selects exit
System.out.printf("\nBye %s and %s, thanks for playing and have a great day!", name1, name2);
bool = false; // ends main while loop, ending the program
}
else { // loops if user does not choose a valid option from the menu
System.out.println("\nPlease choose a valid option.");
menu = 0;
}
}
}
}
代码的问题在下面的部分。程序应该请求用户输入,但它运行 else
语句,然后再次循环。但是,这仅在 answer = s.nextLine();
时发生。如果我将代码更改为 answer = s.next()
,错误就会消失。为了重现问题,您可以运行游戏,然后等待再次播放屏幕出现。程序会要求您再次播放,但在您输入任何响应之前,它会说Please enter a valid answer
,然后再次循环。似乎字符串中已经有一些输入,但字符串是空的。有人可以帮帮我吗?为什么它只在代码为 else
时才运行 answer = s.nextLine();
语句而不询问用户?任何帮助将不胜感激。
menu = 4;
while (menu == 4) {
// asks user to play again
String answer = ""; // local variable, no need for global variable
System.out.println("\nWould you like to play again? y/n");
answer = s.nextLine();
if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y"))
menu = 2; // restarts game if user chooses to play again
else if (answer.equalsIgnoreCase("no") || answer.equalsIgnoreCase("n"))
menu = 0; // goes back to main menu if user chooses not to play again
else
System.out.println("Please enter a valid answer.");
}