为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Java中使用do-while循环制作Magic 8 Ball程序?

如何在Java中使用do-while循环制作Magic 8 Ball程序?

慕婉清6462132 2023-12-13 17:05:16
我正在尝试创建一个 Magic 8 Ball 程序:使用初始值设定项列表将所有响应存储在字符串数组中。使用随机对象(范围在 0 到 19 之间)生成响应的随机数。提示用户输入问题。使用随机数来访问并显示相应的响应。询问用户是否想问另一个问题(使用 do-while 循环)。只要用户说“是”,代码就应该重复。我在执行第 5 步时遇到问题。所有其他步骤我都能够很好地完成,但是当我输入“是”时,它不会让我问另一个问题,而是会跳过该问题并给我答案。这是我到目前为止所拥有的: //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非常感谢您对我的帮助!
查看完整描述

2 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

该问题的正确实现如下:


          //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()为 a.nextLine()以让用户决定继续。


我刚刚在底部做了另一个小更新,以避免您添加的令人困惑的条件,因此yes = 1and no = 0。


查看完整回答
反对 回复 2023-12-13
?
慕仙森

TA贡献1827条经验 获得超7个赞

  • 您有两个嵌套的 while 循环。你只需要一个。

  • 使用 nextLine() -这是你的主要错误

  • 我还将你的 int 转换为布尔值

这是代码:

package eu.webfarmr;


import java.util.Random;

import java.util.Scanner;


public class Question {

    public static void main(String[] args) {

        // initializing variables

        boolean continueAsking = true;

        String otherQ;


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

        do{

            System.out.print("What is your question? ");

            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();


            continueAsking = !otherQ.equalsIgnoreCase("no");


        } while (continueAsking);

        scan.close();

    }

}


查看完整回答
反对 回复 2023-12-13
  • 2 回答
  • 0 关注
  • 60 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信