簡體   English   中英

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

[英]How to use do-while loop to make Magic 8 Ball program in Java?

我正在嘗試創建一個 Magic 8 Ball 程序:

  1. 使用初始化列表將所有響應存儲在字符串數組中。

    1. 使用隨機 object(范圍 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

非常感謝您對我的幫助!

  • 您有兩個嵌套的 while 循環。 你只需要一個。
  • 使用 nextLine() -這是你的主要錯誤
  • 我還將您的 int 切換為 boolean

這是代碼:

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

這個問題的正確實現如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM