gpt4 book ai didi

java - java中如何使用do-while循环来制作魔术八球程序?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:37 24 4
gpt4 key购买 nike

我正在尝试创建一个 Magic 8 Ball 程序:

  1. 使用初始化列表将所有响应存储在字符串数组中。

    1. 使用随机对象(在 0 到 19 范围内)为响应生成一个随机数。

    2. 提示用户输入问题。

    3. 使用随机数访问并显示相应的响应。

    4. 询问用户是否想问另一个问题(使用 do-while 循环)。只要用户说"is",代码就应该重复。

我在执行第 5 步时遇到问题。

我已经能够很好地完成所有其他步骤,但是当我输入"is"时,它没有让我问另一个问题,而是跳过那个问题并给我答案。

这是我目前所拥有的:

  //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

非常感谢您对我的帮助!

最佳答案

这个问题的正确实现如下:

          //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 = 1no = 0

关于java - java中如何使用do-while循环来制作魔术八球程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58531836/

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