import java.util.Scanner;

public class Lab4_5 {
    public static void main(String[]args) {
        Scanner scan= new Scanner(System.in);

        int rows=0;
        int rowIndex=0, colIndex=0;
        boolean choice1= true;
        String y="y";
        String n="n";
        boolean first = true;

        while (choice1==true) {
            if (first==true) {
                first=false;
                System.out.println("Do you want to start(Y/N): ");
            } else if (first==false) {
                System.out.println("Do you want to continue(Y/N): ");
            }

            String choice2=scan.next();
            if (choice2.equals(y)) {
                System.out.println("How many rows/columns(5-21)?");
                rows=scan.nextInt();
                while (rows<5 || rows>21) {
                    System.out.println("That is either out of range or not an integer, try again! ");
                    rows=scan.nextInt();
                }

                System.out.println("What character?");
                String choice3=scan.next();
                System.out.println(" ");

                for (rowIndex=1; rowIndex<=rows; rowIndex++) {
                    for (colIndex=1; colIndex<=rows; colIndex++) {
                        if (rowIndex==1 || rowIndex==rows || colIndex==1 || colIndex==rows) {
                            System.out.print(choice3);
                        } else {
                            System.out.print(" ");
                        }
                    }
                    System.out.println();
                }
            } else if(choice2.equals(n)) {
                choice1 = false;
                System.out.println("Thank you. Goodbye.");
            } else {
                System.out.println("Please either enter Y or N.");
            }
        }
    }//end of main
}

该代码打印了我需要打印的内容,但是当它询问要捕获多少行/列以捕获是否输入除整数以外的代码时,我也必须在代码中包含一些内容(在下面的部分中)。需要一些帮助,我们在捕获异常方面还没有做任何事情,我也不知道如何开始。
String choice2=scan.next();
if (choice2.equals(y)) {
    System.out.println("How many rows/columns(5-21)?");
    rows=scan.nextInt();
    while (rows<5 || rows>21) {
        System.out.println("That is either out of range or not an integer, try again! ");
        rows=scan.nextInt();
    }
}

最佳答案

您需要了解this,请仔细阅读。

基本的理解是

try {
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught.
}

还有一个finally块,即使有错误,该块也将始终执行。它是这样使用的
try {
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught & the returned.
} finally {
  // This will always execute if there is an exception or no exception.
}

在您的特定情况下,您可以具有以下异常(exception)(link)。

InputMismatchException-如果下一个标记与Integer正则表达式不匹配或超出范围
NoSuchElementException-如果输入已用尽
IllegalStateException-如果此扫描仪已关闭

因此,您需要捕获类似的异常
try {
   rows=scan.nextInt();
} catch (InputMismatchException e) {
  // When the InputMismatchException is caught.
  System.out.println("The next token does not match the Integer regular expression, or is out of range");
} catch (NoSuchElementException e) {
  // When the NoSuchElementException is caught.
  System.out.println("Input is exhausted");
} catch (IllegalStateException e) {
  // When the IllegalStateException is caught.
  System.out.println("Scanner is close");
}

09-30 21:05