字符串不包含任何内容或用户只是按回车键

String contains nothing or the user just presses enter

我一直在寻找解决方案并在我的代码上进行了尝试,但仍然没有找到任何运气。我想要做的是,如果用户什么都不输入,只要按下回车键,就会抛出一个异常,一旦抛出异常,就会执行一个 catch 块。但就我而言,它只执行我拥有的第二个捕获块,即 InvalidAnswerException 而不是 BlankException

代码:

import java.util.Scanner;

public class QnA {

public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(System.in);
    
    String[] questions = { "Question-1: Who is the national hero of the Philippines?", "Question-2: What is the national Anthem of the Philippines ?",
            "Question-3: What is the national sport of the philippines ?"};
    String choices[][] = { { "A)Jose Rizal", "B)Andres Bonifacio", "C)Lapu-Lapu" },
             { "A)Paro-Paro G", "B)Bahay kubo", "C)Lupang Hinirang" }, { "A)Basketball", "B)Arnis", "C)Badminton" }, };
    char answers[] = {'A', 'B', 'C'};
    
    int score = 0;
    
    for (int i = 0; i < 3; i++) {
        while (true) {
            System.out.println(questions[i]);
            for (int j = 0; j < 3; j++) 
                 System.out.println(choices[i][j]);
                 System.out.print("Your Answer : ");
                 String ans = sc.nextLine();
                 try {
                     if (ans.contains("A") || ans.contains("a") || ans.contains("B") || ans.contains("b") || ans.contains("C") || ans.contains("c")) {
                         score++;
                         break;
                     }

                     if (ans.matches("[0-9]") || ans.matches("[!@#$%&*()_+=|<>?{}\\[\\]~-]")) {
                         throw new InvalidInputException("");
                     }
                     
                     if (!ans.equalsIgnoreCase("A") || !ans.equalsIgnoreCase("B") || !ans.equalsIgnoreCase("C")) {
                         throw new InvalidAnswerException("");
                     }
                     
                     if (ans.length() == 0) {
                         throw new BlankException("");
                     }
                     
                 } catch (InvalidInputException e) {
                     System.out.println("InvalidInputException,Try again..");
                     continue;
                 } catch (InvalidAnswerException e) {
                     System.out.println("InvalidAnswerException,Try again..");
                     continue;
                 } catch(BlankException e) {
                     System.out.println("BlankException,Try again..");
                     continue;
                 }
        }

    }
                 System.out.println("Your score out of " + questions.length + " is : " +score);
                 sc.close();
}
}

class InvalidInputException extends Exception {
    
    public InvalidInputException(String message) {
        super(message);
    }
}

class InvalidAnswerException extends Exception {
    
    public InvalidAnswerException(String message) {
        super(message);
    }
}

class BlankException extends Exception {
    
    public BlankException(String message) {
        super(message);
    }
}


    

作为@Ole V.V。指出,您的最后一个 if (ans.legnth() == 0) 条件没有应用,因为您的程序 它甚至没有达到 。当用户什么都不输入时 "" 您的 ans 变量变为 ans="" 然后它适用于第三个条件 if (!ans.equalsIgnoreCase("A") || !ans.equalsIgnoreCase("B") || !ans.equalsIgnoreCase("C"))。最简单的解决方案是将最后一个 if 放在其他任何之前。因此,您的代码可能会像这样更改:

while (true) {
            System.out.println(questions[i]);
            for (int j = 0; j < 3; j++) 
                 System.out.println(choices[i][j]);
                 System.out.print("Your Answer : ");
                 String ans = sc.nextLine();
                 try {
                     if (ans.contains("A") || ans.contains("a") || ans.contains("B") || ans.contains("b") || ans.contains("C") || ans.contains("c")) {
                         score++;
                         break;
                     }


                     if (ans.length() == 0) {
                         throw new BlankException("");
                     }
                     

                     if (ans.matches("[0-9]") || ans.matches("[!@#$%&*()_+=|<>?{}\\[\\]~-]")) {
                         throw new InvalidInputException("");
                     }
                     
                     if (!ans.equalsIgnoreCase("A") || !ans.equalsIgnoreCase("B") || !ans.equalsIgnoreCase("C")) {
                         throw new InvalidAnswerException("");
                     }
                     
                 } catch (InvalidInputException e) {
                     System.out.println("InvalidInputException,Try again..");
                     continue;
                 } catch (InvalidAnswerException e) {
                     System.out.println("InvalidAnswerException,Try again..");
                     continue;
                 } catch(BlankException e) {
                     System.out.println("BlankException,Try again..");
                     continue;
                 }
        }