Search code examples
javaloopswhile-loopsentinel

Program doesn't exit when I hit n or N?


I have the following homework problem:

Q1. Use nested for loops statements to draw empty boxes of any character (input from user). The boxes have the same number of rows and columns (input from the user; valid range: 5 to 21). Test for errors in input (including type)

SAMPLE OUTPUT:

Do you want to start(Y/N): y
How many chars/last row? n
Not an integer! Try again! How many chars/last row? fgfgfg
Not an integer! Try again! How many chars/last row? 7.6
Not an integer! Try again! How many chars/last row? 34
ERROR! Valid range 5 - 21. How many chars/last row? 7
What character? k

Do you want to continue(Y/N): y

I've written the below code, but it doesn't exit when I hit 'n' or 'N', and I'm not sure why. How would I fix this?

public static void main(String[] args) {
    Scanner input = new Scanner(System. in );
    char answer = 'n';
    int row = 0;
    char output = 'k';

    do {
        System.out.println("DO YOU WANT TO START Y OR N?");
        answer = input.next().charAt(0);
        System.out.println("enter the number of rows");

        while (!input.hasNextInt()) {
            System.out.println("Not an integer,try again ");
            input.next();
        }

        row = input.nextInt();


        while (row < 5 || row > 21) {
            System.out.println("ERROR! Valid range 5 - 21. How many chars/last row?");
            row = input.nextInt();
        }
        System.out.println("WHAT CHARACTER?");
        output = input.next().charAt(0);

        for (int i = 0; i < row; i++) { //nested for loop to create the box
            System.out.print(output);
        }
        System.out.println();

        for (int i = 0; i < row - 2; i++) {
            System.out.print(output);
            for (int j = 0; j < row - 2; j++) {
                System.out.print(" ");
            }
            System.out.print(output);
            System.out.println();
        }
        for (int i = 0; i < row; i++) {
            System.out.print(output);
        }
        System.out.println();
        System.out.println();
        System.out.println("DO YOU WANT TO CONTINUE ? Y OR N");
        answer = input.next().charAt(0);

    } while (answer == 'Y' || answer == 'y');

    input.close();
    System.out.println("game stop");
}

Solution

  • You need to add condition for N after Do you want to start(Y/N): and Do you want to continue(Y/N):

    System.exit(0) is used to terminate the program.

    Put this code

    System.out.println("DO YOU WANT TO START Y OR N?");
        answer = input.next().charAt(0);
        if(answer == n || answer == N){
            System.exit(0);
        }
    

    And this for Do you want to continue(Y/N):

    System.out.println("DO YOU WANT TO CONTINUE ? Y OR N");
        answer = input.next().charAt(0);
        if(answer == n || answer == N){
            System.exit(0);
        }
    

    Edit

    If you want to print 'Game Stop' if the answer is N, then use Thread.sleep(timeInMilliseconds); before System.exit(0)

    if(answer == n || answer == N){
        Thread.sleep(5000); //This will make console wait for 5 seconds before exiting.
        System.out.println("Game Stop."); //game stop will be printed for 5 seconds
        System.exit(0);
    }