繁体   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