如何使用do-while循环,使魔术8球程序在Java中?

马克·桑托斯:

我想创建一个神奇八号球程序:

  1. 存储所有在一个String数组,使用初始化列表的响应。

    1. 生成用于使用随机对象(取值范围为0〜19)的响应的随机数。

    2. 提示用户输入的问题。

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

    4. 如果他们要问一个问题(使用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

太感谢你对我的帮助!

Bergis:

对于这一问题的正确实施情况如下:

          //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环只需要一个条件在结束do部分。

你的逻辑是在正确的方向,得到了用户的问题,得到的答案,然后问他们是否要问另外一个问题。

此外,交换.next().nextLine()用于获取用户决定继续。

我只是在底部形成另一个小更新,以避免您添加,使混乱的条件语句yes = 1no = 0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

do-while循环对战For循环在Java中的计数

如何在Java中退出while循环?

如何使用Do-While循环来构造程序

如何在Java中的树中找到最长的单词(没有循环(for,while,do ...))

使用while循环在Java中绘制点

使用JOptionPane在Java中执行while循环

编辑程序以使用Java中用户输入的do While / While循环

魔术8球的循环中有错误

如何在do / while循环中正确使用函数?

如何在C / C ++中的SWITCH-CASE结构内使用do-while循环从头开始重新启动程序?

Java Do-While循环-如何使用Q选项结束程序

Java on JCreator(使用while循环创建LEAP YEAR程序)

使用的do while循环如何提供用户提示?

如何使用Java中的if-else语句进行do ... while循环?

我如何使用嵌套的while循环?在java中

如何解决这个基本程序?Java do-while循环

如何在Java中创建魔术方块?

如何在Java中限制while循环?

如何使用 Powershell 创建适当的 do-while 循环

魔术 8 球程序循环无法按预期工作

如何在不使用“goto”和“do while”循环的情况下进入程序的初始阶段?

当用户输入“退出”一词时,如何使用 Java 中的扫描器退出 do-while 循环?

Java 中的简单 Do-while 循环

如何在 do-while 循环中使用 getline?

如何使用带分区的 DO 循环

如何使用“Do While”循环遍历列表

Java,如何使用多字符布尔表达式关闭 do-while 循环?

如何使用 Do While 停止循環?

如何在java中的do while循环的“while”部分打印出来?