亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在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條經驗 獲得超8個贊

  • 您有兩個嵌套的 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 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號