java - “no”输入函数在 while 循环 Java 中不起作用
问题描述
该程序应验证用户输入。
游戏应该要求用户再次玩,如果是则继续,如果不是则停止。
一旦用户停止游戏,程序应该打印计算机和用户的总获胜次数。当我输入“no”时,它会打印“Type 1 for rock,...”而不是打印乐谱。下面是我的代码:
import java.util.*; public class rockPaperScissors { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Do you want to play a round of rock, paper, scissors? Type yes or no."); String YoN = input.nextLine();//yes or no repsonse int Uwin = 0;//user win count int Cwin = 0;//computer win count int tie = 0;//tie count if (YoN.equalsIgnoreCase("yes")) { while (!YoN.equalsIgnoreCase("no")) { System.out.println("Type 1 for rock, 2 for paper, and 3 for scissors."); Scanner console = new Scanner(System.in); int choice = console.nextInt(); while (choice < 1 || choice > 3) { System.out.println("Invalid entry. Please type 1, 2, or 3."); choice = console.nextInt(); } Random r = new Random(); int comp = r.nextInt(3) + 1; if (comp == 1) { System.out.println("Computer chose rock."); } if (comp == 2) { System.out.println("Computer chose paper."); } if (comp == 3) { System.out.println("Computer chose scissors."); } if ((choice == 3 && comp == 1) || (choice == 2 && comp == 3) || (choice == 1 && comp == 2)) { Cwin++; } else if ((choice == 2 && comp == 1) || (choice == 3 && comp == 2) || (choice == 1 && comp == 3)) { Uwin++; } else { tie++; } System.out.println("Computer wins: " + Cwin + "\nYour wins: " + Uwin + "\nTies: " + tie); System.out.println("\n\nDo you want to play a round of rock, paper, scissors?"); YoN = console.nextLine(); console.nextLine(); if (YoN.equalsIgnoreCase("no")) { System.out.println("Computer wins: " + Cwin + "\nYour wins: " + Uwin + "\nTies: " + tie); } } } else if (YoN.equalsIgnoreCase("no")) { System.out.println("Thank you!"); } else { System.out.println("Invalid entry. Please type yes or no."); YoN = input.nextLine(); } }
}
解决方案
这行得通。我已将行更改YoN = console.nextLine();
为YoN = input.nextLine();
.
import java.util.*;
public class rockPaperScissors
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Do you want to play a round of rock, paper, scissors? Type yes or no.");
String YoN = input.nextLine();//yes or no repsonse
int Uwin = 0;//user win count
int Cwin = 0;//computer win count
int tie = 0;//tie count
if (YoN.equalsIgnoreCase("yes"))
{
while (!YoN.equalsIgnoreCase("no"))
{
System.out.println("Type 1 for rock, 2 for paper, and 3 for scissors.");
Scanner console = new Scanner(System.in);
int choice = console.nextInt();
while (choice < 1 || choice > 3)
{
System.out.println("Invalid entry. Please type 1, 2, or 3.");
choice = console.nextInt();
}
Random r = new Random();
int comp = r.nextInt(3) + 1;
if (comp == 1)
{
System.out.println("Computer chose rock.");
}
if (comp == 2)
{
System.out.println("Computer chose paper.");
}
if (comp == 3)
{
System.out.println("Computer chose scissors.");
}
if ((choice == 3 && comp == 1) || (choice == 2 && comp == 3) || (choice == 1 && comp == 2))
{
Cwin++;
}
else if ((choice == 2 && comp == 1) || (choice == 3 && comp == 2) || (choice == 1 && comp == 3))
{
Uwin++;
}
else
{
tie++;
}
System.out.println("Computer wins: " + Cwin + "\nYour wins: " + Uwin + "\nTies: " + tie);
System.out.println("\n\nDo you want to play a round of rock, paper, scissors?");
YoN = input.nextLine();
//console.nextLine();
if (YoN.equalsIgnoreCase("no"))
{
System.out.println("Computer wins: " + Cwin + "\nYour wins: " + Uwin + "\nTies: " + tie);
}
}
}
else if (YoN.equalsIgnoreCase("no"))
{
System.out.println("Thank you!");
}
else
{
System.out.println("Invalid entry. Please type yes or no.");
YoN = input.nextLine();
}
}
}
推荐阅读
- laravel - 如何在 laravel 控制器中使用 isset 条件
- typescript - 如何防止派生类属性遮蔽基类属性
- keras - 如何在 kerase>2.0 中使用 Merge?
- sequelize.js - sequelize:如何创建约束名称
- ios - wkwebview 100%丢包预设
- php - 由于 ajax 导致“未选择文件”错误,我的文件输入字段不断重置
- php - 如何在 php 中的 mform 按钮上运行功能
- regression - 如何将回归线拟合到 seaborn 散点图中
- c++ - 使用 dll 源代码 C++ 调试 dll 文件
- javascript - 如何从 Javascript 向 Streamlit 发送数据?