-2

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");
}
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
happy face
  • 43
  • 9

2 Answers2

2

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);
}
Apurva
  • 7,871
  • 7
  • 40
  • 59
  • 1
    You can use `System.exit(0);`, or you can use `break;` instead, if you want to print that last `System.out.println("game stop");`. – miselking Feb 12 '15 at 17:05
  • 1
    @ChhetriCamy see I edited my answer and added how to print 'game stop' – Apurva Feb 12 '15 at 17:16
0

The simplest way to do this is:

Check for input "N" INSIDE the loop, and break out of the while loop, possibly like so:

if ((answer == 'n') || (answer == 'N')) {
    break;
}

Also, you're checking for y/n input 2 times in this program. A better method of writing this would be to use a normal while loop instead of a do-while loop; clearly, in the question if you input N right at the beginning you shouldn't be running through the program at all. A Do-While loop is useful to ensure that the program runs at least once (which is not what should happen here; the program should only run if the input is valid eg: "y"). While using a DW-loop is "ok", a while loop would serve the purpose better here.

Your loop can be written like so instead:

// you need this line to initially print the y/n question.
System.out.println("DO YOU WANT TO START Y OR N?");
// get an input and check if it is not n
while (Character.toUpperCase(input.next().charAt(0)) != 'N') {    
    // do stuff here 
    System.out.println("DO YOU WANT TO CONTINUE ? Y OR N"); // ask for next input
}
Aify
  • 3,543
  • 3
  • 24
  • 43