第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在Java中使用do-while循環(huán)制作Magic 8 Ball程序?

如何在Java中使用do-while循環(huán)制作Magic 8 Ball程序?

慕婉清6462132 2023-12-13 17:05:16
我正在嘗試創(chuàng)建一個(gè) Magic 8 Ball 程序:使用初始值設(shè)定項(xiàng)列表將所有響應(yīng)存儲(chǔ)在字符串?dāng)?shù)組中。使用隨機(jī)對(duì)象(范圍在 0 到 19 之間)生成響應(yīng)的隨機(jī)數(shù)。提示用戶輸入問題。使用隨機(jī)數(shù)來訪問并顯示相應(yīng)的響應(yīng)。詢問用戶是否想問另一個(gè)問題(使用 do-while 循環(huán))。只要用戶說“是”,代碼就應(yīng)該重復(fù)。我在執(zhí)行第 5 步時(shí)遇到問題。所有其他步驟我都能夠很好地完成,但是當(dāng)我輸入“是”時(shí),它不會(huì)讓我問另一個(gè)問題,而是會(huì)跳過該問題并給我答案。這是我到目前為止所擁有的: //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);我的預(yù)期結(jié)果是:    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): 我用上面的代碼得到的結(jié)果:    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非常感謝您對(duì)我的幫助!
查看完整描述

2 回答

?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

該問題的正確實(shí)現(xiàn)如下:


          //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 循環(huán),記住do-while循環(huán)只需要該部分末尾的一個(gè)條件do。


你的邏輯方向是正確的,得到用戶的問題,得到答案,然后問他們是否想問另一個(gè)問題。


另外,將 交換.next()為 a.nextLine()以讓用戶決定繼續(xù)。


我剛剛在底部做了另一個(gè)小更新,以避免您添加的令人困惑的條件,因此yes = 1and no = 0。


查看完整回答
反對(duì) 回復(fù) 2023-12-13
?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊

  • 您有兩個(gè)嵌套的 while 循環(huán)。你只需要一個(gè)。

  • 使用 nextLine() -這是你的主要錯(cuò)誤。

  • 我還將你的 int 轉(zhuǎn)換為布爾值

這是代碼:

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

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-12-13
  • 2 回答
  • 0 關(guān)注
  • 164 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)