gpt4 book ai didi

java - 执行不同任务时遇到问题/识别输入

转载 作者:行者123 更新时间:2023-12-01 14:02:37 25 4
gpt4 key购买 nike

我想做的是,当程序询问您是否想添加另一个学生时,它会说“您想添加另一个学生Y/N吗?”如果输入Y,那么它会让您添加另一个,如果您输入N,它会让您返回到我的程序的主菜单。所以我设置了 Y=1N=0 我只是想弄清楚程序如何读取 Y 并知道要做什么做。

public class TestStudent {

public static void main (String[] args) throws IOException
{
InputStreamReader isr = new InputStreamReader (System.in );
BufferedReader stdin = new BufferedReader( isr );


String check, tempString, tempString2, setName, getName, setScore, getScore;
int tempInt, tempInt2, y, n;
boolean quit = false;
y=1;
n=0;
Student s1, s2, s3;
s1 = new Student();
s2 = new Student();
s3 = new Student();

do
{
System.out.println("A - Add student, D - Delete student, F - Find student, H - Help, S - Scores, X - Exit");
check = stdin.readLine().toLowerCase();
switch (check)
{

case "a":
System.out.println("Enter the student's name");
tempString = stdin.readLine();
s1.setName(tempString);
System.out.println("Would you like to add another student Y/N?");
tempString = stdin.readLine();
if (y=1) {
System.out.println("Enter the student's name");
tempString = stdin.readLine();
}
s2.setName(check);
s3.setName(check);
break;

最佳答案

首先,您需要将 toLowerCase 添加到您的 tempString 赋值中。

tempString = stdin.readLine().toLowerCase();
if (tempString.equals("y")) {

这将检查他们给你的字符串与 y,你的版本刚刚更新(= 表示赋值)一个你没有使用的变量。

关于java - 执行不同任务时遇到问题/识别输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19232632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com