简体   繁体   中英

How to show an input is invalid in this java code

I'm trying to make it so that when the user enters anything but y / n it'll say error and when they enter n it'll say have a great day. This is what I have so far, but I keep running into trouble.

This is the assignment:

Write a program that gets an integer from the user, say x, and then prints an x by x square, and it prints that square x number of times. For example, if the user enters 4, your program will print a 4x4 square four distinct times. Specifics:

The user enters a value 3-15. Input validation: only accept 3-15. Allow the user to repeat the program if desired. Input validation: Y or N only, but also allow lowercase entries.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*******************************************************\n"
                + "*******************SQUARE GENERATOR********************\n"
                + "*******************************************************\n"
                + "\nThis program will let you enter an integer between\n"
                + "3-15 and print out that many squares of that dimension.\n");

        char answer = 'y';
        while (answer == 'y' || answer == 'Y') {
            System.out.println("Enter the square size --> ");
            int x = keyboard.nextInt();
            while (x < 3 || x > 15) {
                System.out.println("Error: Select a number between 3 and 15 inclusive: ");
                x = keyboard.nextInt();
            }
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < x; j++) {
                    for (int k = 0; k < x; k++) {
                        System.out.print("X");
                    }
                    System.out.println("");
                }
                System.out.println("");
            }
            System.out.println("Would you like to try again Y/N? --> ");
            answer = keyboard.next().charAt(0);
        }
        answer = 'n';
        while (answer == 'n' || answer == 'N') {
            System.out.println("Program ending. Have a great day.");
        }
        keyboard.close();
    }
}

You can solve this problem by only using one while loop. You use a break condition to inidicate that the loop should terminate (in your example if the user enters 'n').

Here is an example how I would try to solve this problem:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("*******************************************************\n"
            + "*******************SQUARE GENERATOR********************\n"
            + "*******************************************************\n"
            + "\nThis program will let you enter an integer between\n"
            + "3-15 and print out that many squares of that dimension.\n");

    boolean exit = false; // define the boolean variable
    char answer = 'y';

    while (!(exit)) { // start the while loop
        if (answer == 'y' || answer == 'Y') { // if the user enters 'y' proceed with your code
            System.out.println("Enter the square size --> ");
            int x = keyboard.nextInt();
            while (x < 3 || x > 15) {
                System.out.println("Error: Select a number between 3 and 15 inclusive: ");
                x = keyboard.nextInt();
            }
            for (int i = 0; i < x; i++) {
                for (int j = 0; j < x; j++) {
                    for (int k = 0; k < x; k++) {
                        System.out.print("X");
                    }
                    System.out.println("");
                }
                System.out.println("");
            }
            System.out.println("Would you like to try again Y/N? --> ");
            answer = keyboard.next().charAt(0);
        } else if (answer == 'n' || answer == 'N') { // if the user enters 'n' exit the program and the loop
            System.out.println("Program ending. Have a great day.");
            exit = true;
        } else { // display an error message when something else is typed
            System.out.println("You entered an unvalid char, please answer by saying Y/N!");
            answer = keyboard.next().charAt(0);
        }
    }
    System.out.println("Reached end of program!");
    keyboard.close();
}

Since this looks like homework I won't post the full answer but you can change the

while (answer == 'n' || answer == 'N')

to

if (answer == 'n' || answer == 'N')

Also close the scanner inside the if block above. The else case to the above is where you would throw the error. Hope its clear.

EDIT Another thing I would like to add is that you can remove answer = 'n'; before the if condition above. That will already be read by

System.out.println("Would you like to try again Y/N? --> "); answer = keyboard.next().charAt(0);

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