如何询问用户是否想退出程序并用Java打印感谢信息
我正在学习第一个Java课程,并正在进行第二个项目。该项目是关于在虚拟三维工作区上创建一个房间网络程序。每个房间都提供了一个虚拟环境,可以组合成一个模拟或虚拟的世界。 基本上,在程序开始时,我使用while循环,最后我想问用户是否想退出程序,并打印一条感谢消息。但是,while循环不起作用。无论我输入y或n,我的程序都会退出。下面是我的代码如何询问用户是否想退出程序并用Java打印感谢信息,java,Java,我正在学习第一个Java课程,并正在进行第二个项目。该项目是关于在虚拟三维工作区上创建一个房间网络程序。每个房间都提供了一个虚拟环境,可以组合成一个模拟或虚拟的世界。 基本上,在程序开始时,我使用while循环,最后我想问用户是否想退出程序,并打印一条感谢消息。但是,while循环不起作用。无论我输入y或n,我的程序都会退出。下面是我的代码 import java.util.Scanner; public class Project { public static void main(St
import java.util.Scanner;
public class Project
{
public static void main(String[] args)
{
Map map = new Map();
int floor = 0;
int row = 0;
int col = 0;
String input = " ";
Scanner scan = new Scanner(System.in);
// Begin user dialog. Welcome message
System.out.println("Welcome to the L.A Underground! (Verson 1.1)");
System.out.println();
String choice = "y";
while(!input.equalsIgnoreCase("quit"))
{
input = scan.nextLine().toLowerCase();
// My codes are here
if (input.equals("south")
{statement}
else
System.out.println("You can't go that way.");
else if (input.equals("quit"))
{ // See if user wants to continue
System.out.println("Do you wish to leave the Underground (Y/N)? >");
choice = scan.nextLine();
System.out.println();
}
// if user enters other words than quit
else
System.out.println("I don't recognize the word '" + input +"'");
}
System.out.println("Thank you for visiting L.A Underground.");
}
}
当我键入“退出”时,控制台显示消息:“您想离开地铁吗?(Y/N)?>”。我尝试了是/否(是/否)程序终止。感谢您的帮助。多谢各位
更新:很抱歉造成混乱。我希望程序运行的是,当用户键入“退出”时,消息将打印出“您是否希望离开地铁(Y/N)?>?”,如果用户键入“hello”,消息将是“我不理解‘hello’这个词”。当用户键入y时,程序将退出,否则(键入n),程序将重新启动。谢谢大家! 在循环内部请求用户输入。如果
input.equalsIgnoreCase(“退出”)
,则提示用户“您确定”消息。如果输入.equalsIgnoreCase(“y”)
,则中断该循环,否则继续
Scanner scan = new Scanner(System.in);
String input;
// Begin user dialog. Welcome message
System.out.println("Welcome to the L.A Underground! (Verson 1.1)");
System.out.println();
while (true) {
input = scan.nextLine();
if (input.equalsIgnoreCase("quit")) {
System.out.print("Do you wish to leave the Underground (Y/N)? >");
if (scan.nextLine().equals("y")) {
break;
}
}
// input wasn't "quit", so do other stuff here
}
System.out.println("Thank you for visiting L.A Underground.");
你的代码循环,直到它得到“退出”。。。然后问“是/否”。。。然后干脆退出,不管怎样
您需要更改循环,以便它同时包含“此处的我的代码”和“退出y/n”检查
例如:
...
boolean done = false;
while(!done) {
//MY CODES ARE HERE
if (input.equalsIgnoreCase("quit") && getYesNo ()) == 'y') {
done = true;
}
}
“getYesNo()”是您编写的方法。例如:
char getYesNo () {
System.out.print("Do you wish to leave the Underground (Y/N)? >");
String line = scan.nextLine();
return line.charAt(0);
}
在您发布的代码中,您的循环由条件控制!input.equalsIgnoreCase(“退出”)
。也就是说,如果input
为“退出”,则循环终止
但只有当input
为“退出”时,才会执行以下块:
因此,如果执行此块,!input.equalsIgnoreCase(“quit”)
计算结果为false
,循环终止。那不是你想要的
既然您知道了问题所在,修复它就很容易了。检查上述if
块中choice
的值:如果choice
不是yes,不要退出,即将input
重置为默认值
我已经粘贴了工作代码
不确定上述程序是否正确。我没有看到IF,我只看到注释//如果用户输入的不是quit的话。我没有看到您对变量选择中的值做任何处理。我的意思是在你的代码system.out.println(“你想离开地铁(Y/N)?>”;choice=scan.nextLine();您如何处理choiceWelcome to StackOverflow:)中的值请格式化您的代码。它有一个额外的封闭支架。和一个else if
,前面没有if
。另外,正如@AlbertPinto所说的,你已经要求提供y/n,但你没有做任何事情。@AlbertPinto对混淆表示抱歉。我刚刚编辑了我的代码。希望它能澄清你的想法。你能解释一下你对变量选择中的值应该怎么做的想法吗?谢谢@感谢您的欢迎信息!我刚刚编辑了我的代码。在发布之前,我无意中删除了前面的“如果”。我最近才学习Java,所以我可能会对我的演示感到困惑。希望你能理解。谢谢你的帮助!谢谢你的帮助!我试着把它应用到我的代码上,但它不起作用。我不知道为什么while循环不起作用。除此之外,我的程序运行得很好。@pausm4:谢谢你的帮助。但是,我希望程序运行的是,当用户键入“退出”时,消息将打印出“您是否希望离开地铁(Y/N)?>?”,如果用户键入“hello”,消息将是“我不理解‘hello’这个词”。当用户键入y时,程序将退出,否则(键入n),程序将重新启动。1)程序已提示用户“退出”。2) 如果你想在得到“y”或“n”之前说“我不明白”,那么只需在getYesNo()
的内部循环直到“line.equalsIgnoreCase(“y”)或“n”):)OMG..谢谢!它现在起作用了。我忘了检查选项的值,正如你和@Albert Pino之前所说的。再次感谢!我真的感谢你的帮助。
if (input.equals("quit"))
{
// See if user wants to continue
System.out.println("Do you wish to leave the Underground (Y/N)? >");
choice = scan.nextLine();
System.out.println();
}