简体   繁体   中英

Java - while loop not working as expected

I am working with a fairly basic programming task in Java. We are asked to create a chat-robot, where the robot is to answer randomly from a set of given strings until the user writes "Bye!", where the robot will simply reply with "Bye!" and end the program. I've written the following code:

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!");
        }

    } 

}

I have two issues with the program:

  1. At times, seemingly at random, the program will answer with multiple strings.
  2. When writing "Bye!", the program throws in another string before writing "Bye!". I do know that this may be resolved by adding "break;", but I'd consider this bad practice seeing as I am already using a boolean. (I'd like to keep using it.)

I have no idea as to why these errors occur.

Check the condition for exit before printing anything. That will resolve the 2nd issue

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

Change your program like this

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

    } 

}
}

Use below code snappet to solve Issue2:

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM