在此Java代码中,如何显示输入无效

时间:2019-03-14 21:39:15

标签: java

我正在尝试做到这一点,以便当用户输入y / n以外的任何内容时都会说出错误,而当他们输入n时则会说出天。到目前为止,这就是我所遇到的,但是我一直遇到麻烦。

这是作业:

  

编写一个程序,该程序从用户那里获取一个整数,例如x,然后   打印x x x平方,并打印该平方x倍。   例如,如果用户输入4,则您的程序将打印4x4   平方四个不同的时间。具体:

     

用户输入值3-15。输入验证:仅接受3-15。   如果需要,允许用户重复该程序。输入验证:是   或N,但也允许小写字母输入。

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

2 个答案:

答案 0 :(得分:0)

您可以仅使用一个while循环来解决此问题。您使用中断条件来指示循环应终止(在您的示例中,如果用户输入“ n”)。

这是我如何尝试解决此问题的示例:

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

答案 1 :(得分:0)

由于这看起来像是家庭作业,因此我不会发布完整答案,但是您可以更改

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

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

还关闭上面if块内的扫描仪。上面的其他情况是您将引发错误的地方。希望它清除。

编辑 我想补充的另一件事是,您可以在上述answer = 'n';条件之前删除if

已经可以读取

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