1

我正在处理一个相当基本的 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;”来解决,但我认为这是一种不好的做法,因为我已经在使用布尔值了。(我想继续使用它。)

我不知道为什么会发生这些错误。

4

5 回答 5

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]);
 } 
于 2013-09-23T10:29:16.003 回答
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]);
        }

    } 

}
}
于 2013-09-23T10:39:41.937 回答
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!");
        }
    } 
于 2013-09-23T10:49:29.180 回答
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]);
        }
    }
于 2013-09-23T10:53:04.197 回答
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
于 2014-10-25T06:14:55.907 回答