如何使用DoWhile循环在Java中制作Magic 8 Ball程序?
我正在尝试创建一个神奇的8球程序:如何使用DoWhile循环在Java中制作Magic 8 Ball程序?,java,java.util.scanner,do-while,Java,Java.util.scanner,Do While,我正在尝试创建一个神奇的8球程序: 使用初始值设定项列表将所有响应存储在字符串数组中 使用随机对象(范围为0到19)为响应生成随机数 提示用户输入问题 使用随机数访问并显示相应的响应 询问用户是否想问另一个问题(使用do while循环)。只要用户说“是”,代码就应该重复 我在步骤5中遇到问题 所有其他步骤我都能很好地完成,但当我输入“是”时,它没有让我问另一个问题,而是跳过了这个步骤,给了我答案 以下是我到目前为止的情况: //initializing variables int s
//initializing variables
int stop = 0;
String otherQ,q;
//initializing array
String[] responses = {
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."};
//creates objects
Scanner scan = new Scanner (System.in);
Random rn = new Random();
//input
//THIS IS WHERE I AM HAVING A PROBLEM.
do {
System.out.print("What is your question? ");
q = scan.nextLine();
System.out.println(responses[rn.nextInt(19)]); //method caller
while (stop == 0) {
System.out.print("Would you like to ask another question? (Answer yes or no): ");
otherQ = scan.next();
if (otherQ.equalsIgnoreCase("yes")){
break;
}else if (otherQ.equalsIgnoreCase("no")){
stop = 1;
}
}
} while (stop == 0);
我的预期结果是:
What is your question? Question goes here
As I see it, yes.
Would you like to ask another question? (Answer yes or no): yes
What is your question? Cannot predict now.
Would you like to ask another question? (Answer yes or no):
我通过上面的代码得到的结果:
What is your question? Question goes here
It is certain.
Would you like to ask another question? (Answer yes or no): yes
What is your question? Question goes here
Would you like to ask another question? (Answer yes or no): no
非常感谢你帮助我 - 您有两个嵌套的while循环。你只需要一个李>
- 使用nextLine()-这是您的主要错误
- 我还把你的int换成了boolean
package eu.webfarmr;
导入java.util.Random;
导入java.util.Scanner;
公开课问题{
公共静态void main(字符串[]args){
//初始化变量
布尔连续搜索=真;
字符串otherQ;
//初始化数组
String[]responses={“这是肯定的”,“这绝对是肯定的”,“毫无疑问”,“是的-肯定的”,
“你可以信赖它。”,“依我看,是的。”,“最有可能。”,“前景良好。”,“是的。”,
“符号指向是。”,“回答模糊,再试一次。”,“稍后再问。”,“最好现在不要告诉你。”,
“现在无法预测”,“集中精力再问一次”,“别指望它”,“我的回答是否定的”,
“我的消息来源说没有。”,“前景不太好。”,“非常可疑。”};
//创建对象
扫描仪扫描=新扫描仪(System.in);
Random rn=新的Random();
//输入
做{
System.out.print(“您的问题是什么?”);
scan.nextLine();
System.out.println(响应[rn.nextInt(19)];//方法调用方
System.out.print(“您想问另一个问题吗?(回答是或否):”;
otherQ=scan.nextLine();
continueAsking=!otherQ.equalsIgnoreCase(“否”);
}同时(继续观察);
scan.close();
}
}
此问题的正确解决方法如下:
//initializing variables
int stop = 0;
String otherQ,q;
//initializing array
String[] responses = {
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."};
//creates objects
Scanner scan = new Scanner (System.in);
Random rn = new Random();
//input
//THIS IS WHERE I AM HAVING A PROBLEM.
do {
System.out.print("What is your question? ");
q = scan.nextLine();
System.out.println(responses[rn.nextInt(19)]); //method caller
System.out.print("Would you like to ask another question? (Answer yes or no): ");
otherQ = scan.nextLine();
} while (otherQ.equalsIgnoreCase("yes"));
您可以在do-while中删除嵌套的while循环,记住do-while
循环只需要do
部分末尾的一个条件
你的逻辑是正确的,得到用户的问题,得到答案,然后问他们是否想问另一个问题
另外,将.next()
切换为.nextLine()
,以获得用户继续的决定
我只是在底部做了另一个小的更新,以避免您添加的混淆条件,因此
yes=1
和no=0
但是我想使用do-while循环,所以当这个人说他们想问另一个问题时,它会重复“你的问题是什么?”就像我在我发布的问题上给你的预期结果一样。当然你可以使用do while。。。我已经更新了代码。变化不大。Do while保证至少执行一次,而while(…)将首先测试该条件。此外,@DavidBrossard在调用Scanner.nextLine()
之前添加下一行状态检查,否则您将得到NoTouchElementException
谢谢您,这帮了大忙!在调用Scanner.nextLine()
或直接调用Scanner.nextLine()
之前,我添加了两个hasNextLine()
检查,如果不检查是否存在下一行,程序将抛出java.util.NoSuchElementException
,表示找不到行。除此之外,这一次的逻辑很好。这个编辑实际上不起作用。首先,如果在if
语句中包装otherQ
的赋值以检查扫描仪是否有下一个输入行,那么while
条件将有编译错误,因为otherQ
现在未定义。另外,据我所知,这里不需要hasNextLine()
?我们不是从文件或任何其他输入读取输入,而是从控制台获取输入。如果这是错误的,请有人告诉我,我们可以修复编译错误,包括这些检查。