簡體   English   中英

Java-while循環未按預期工作

[英]Java - while loop not working as expected

我正在用Java進行相當基本的編程任務。 我們被要求創建一個聊天機器人,在該機器人中,機器人將從一組給定的字符串中隨機應答,直到用戶寫下“ Bye!”為止,在該機器人中,機器人將簡單地回答“ Bye!”。 並結束程序。 我編寫了以下代碼:

import java.util.Scanner;
import java.util.Random;

public class Robot {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Random random = new Random();

    String[] answer = new String[6];
    answer[0] = "blabla1";
    answer[1] = "blabla2";
    answer[2] = "blabla3";
    answer[3] = "blabla4";
    answer[4] = "blabla5";
    answer[5] = "blabla6";
    boolean keepGoing = true;


    System.out.println("Hello, how can I help you?");
    while (keepGoing) {
        String input = in.next();
        int output = random.nextInt(6);
        System.out.println(answer[output]);

        if (input.equals("Bye!")){
            keepGoing = false;
            System.out.println("Bye!");
        }

    } 

}

我的程序有兩個問題:

  1. 有時,看似隨機的,程序會回答多個字符串。
  2. 編寫“ Bye!”時,程序將在編寫“ Bye!”之前引發另一個字符串。 我確實知道可以通過添加“ break;”來解決此問題,但是由於我已經使用了布爾值,因此我認為這是一種不好的做法。 (我想繼續使用它。)

我不知道為什么會出現這些錯誤。

打印任何東西之前,請檢查退出條件。 那將解決第二個問題

while (true) {
    String input = in.next();

    if (input.equals("Bye!")){
        System.out.println("Bye!"); 
        break;           
    }
    int output = random.nextInt(6);
    System.out.println(answer[output]);
 } 

像這樣更改程序

import java.util.Scanner;
import java.util.Random;

public class Robot {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Random random = new Random();

    String[] answer = new String[6];
    answer[0] = "blabla1";
    answer[1] = "blabla2";
    answer[2] = "blabla3";
    answer[3] = "blabla4";
    answer[4] = "blabla5";
    answer[5] = "blabla6";
    boolean continue1 = true;


    System.out.println("Hello, how can I help you?");
    while (continue1) {
        String input = in.next();
        int output = random.nextInt(6);
       if (input.equals("Bye!")){
        continue1 = false;
            System.out.println("Bye!");
        }else
        {
             System.out.println(answer[output]);
        }

    } 

}
}

使用下面的代碼片段來解決問題2:

    while (keepGoing) {
        String input = in.next();
        int output = random.nextInt(6);
        if(!input.equals("Bye!"))
            System.out.println(answer[output]);

        if (input.equals("Bye!")){
            keepGoing = false;
            System.out.println("Bye!");
        }
    } 
 Scanner in = new Scanner(System.in);
    Random random = new Random();

    String[] answer = new String[6];
    answer[0] = "blabla1";
    answer[1] = "blabla2";
    answer[2] = "blabla3";
    answer[3] = "blabla4";
    answer[4] = "blabla5";
    answer[5] = "blabla6";
    boolean keepGoing = true;

    System.out.println("Hello, how can I help you?");
    while (keepGoing) {
        String input = in.next();
        if ("Bye!".equals(input)) {
            keepGoing = false;
            System.out.println("Bye!");
        } else {
            int output = random.nextInt(6);
            System.out.println(answer[output]);
        }
    }
import java.util.Scanner;
import java.util.Random;

public class Robot 
{

    public static void main(String[] args) 
    {
            Scanner in = new Scanner(System.in);
            Random random = new Random();

            String[] answer = new String[6];
            answer[0] = "blabla1";
            answer[1] = "blabla2";
            answer[2] = "blabla3";
            answer[3] = "blabla4";
            answer[4] = "blabla5";
            answer[5] = "blabla6";
            boolean keepGoing = true;


            System.out.println("Hello, how can I help you?");
            while (keepGoing) 
            {
                String input = in.next();
                int output = random.nextInt(6);
                System.out.println(answer[output]);

                if (input.equals("Bye!"))
                {
                        keepGoing = false;
                        System.out.println("Bye!");
                } //This bracket is the only thing missing in your code.
            }// End of while loop

        } // End of main method

}// End of class

暫無
暫無

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

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