Java - while循环不按预期工作

时间:2013-09-23 10:22:42

标签: java while-loop boolean

我正在使用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. 当写“再见!”时,程序在写“再见!”之前会抛出另一个字符串。我知道这可以通过添加“break;”来解决,但我会考虑这个不好的做法,因为我已经在使用布尔值。 (我想继续使用它。)
  3. 我不知道为什么会出现这些错误。

5 个答案:

答案 0 :(得分:1)

在打印任何内容之前检查退出条件。这将解决第二个问题

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

答案 1 :(得分:1)

像这样更改你的程序

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 :(得分:0)

使用下面的代码段来解决问题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!");
        }
    } 

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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