java - 为什么我的代码在请求用户输入时会跳到 else 语句?
问题描述
我制作了一个与 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.");
}
解决方案
推荐阅读
- python - Python/Flask - 通过从 Jupyter Notebook 传递路径来上传文件
- ubuntu - “ 404 Not Found [IP: ] ” 尝试安装任何 ros 动力学包时
- mysql - PHP 错误?:MySQL (MariaDB 10.4.8) 给我“非法混合排序规则”,但列是相同的排序规则类型
- reactjs - 如何测试父组件中存在的 onClick 功能(箭头功能)?
- amazon-s3 - 从 Spark 结构化流应用程序写入 S3 的性能问题
- fortran - 如何解决 Fortran 运行时错误:文件结束?
- kotlin - 如何在 Kotlin 中检查序列是否为空
- node.js - IIS通过https问题反向代理到nodejs应用程序
- javascript - 我可以在反应功能组件中使用“让”吗?
- javascript - 如何跳过 Highcharts 中的数据点而不导致图表行中断?