Untitled
unknown
plain_text
2 years ago
2.7 kB
13
Indexable
import java.util.Scanner;
// USER DEFINED EXCEPTIONS
class OnlyABCException extends Exception {
OnlyABCException() {
super("ABC LANG OK!!!!!!!! TRY AGAIN");
}
}
class NoNumAndSpecCharException extends Exception {
NoNumAndSpecCharException() {
super("BAWAL NUMBER AT IBANG CHARACTER. TRY AGAIN");
}
}
class EmptySpaceException extends Exception {
EmptySpaceException() {
super("WALA KANG SINULAT. TRY AGAIN");
}
}
// MAIN CLASS
public class QuizBee {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double totalScore = 0;
// QUESTIONS
String[][] questions = {
{"(QUESTION) \nA. \nB. \nC.", "A"},
{"(QUESTION) \nA. \nB. \nC.", "B"},
{"(QUESTION) \nA. \nB. \nC.", "C"},
{"(QUESTION) \nA. \nB. \nC.", "A"},
{"(QUESTION) \nA. \nB. \nC.", "C"},
{"(QUESTION) \nA. \nB. \nC.", "A"},
{"(QUESTION) \nA. \nB. \nC.", "A"},
{"(QUESTION) \nA. \nB. \nC.", "C"},
{"(QUESTION) \nA. \nB. \nC.", "B"},
{"(QUESTION) \nA. \nB. \nC.", "B"},
};
// OPENING
System.out.println("Are you [REDACTED] quiz \n(my lawyer advised me not to complete the sentence)");
System.out.println("Type the letter of your answer ^_______^ \n(A, B, or C)");
// LOOP FOR QNA
for (int i = 0; i < questions.length; i++) {
try {
System.out.println((i + 1) + ".) " + questions[i][0]);
String answer = scanner.nextLine().toUpperCase();
if (answer.isEmpty()) {
throw new EmptySpaceException();
} else if (!answer.matches("[ABC]")) {
if (answer.matches(".*[0-9\\W].*")) {
throw new NoNumAndSpecCharException();
} else {
throw new OnlyABCException();
}
}
if (answer.equalsIgnoreCase(questions[i][1])) {
totalScore += 10;
}
} catch (EmptySpaceException e) {
System.out.println(e.getMessage());
i--; // Repeat the same question
} catch (OnlyABCException e) {
System.out.println(e.getMessage());
i--; // Repeat the same question
} catch (NoNumAndSpecCharException e) {
System.out.println(e.getMessage());
i--; // Repeat the same question
}
}
System.out.println("Total Score: " + totalScore);
}
}
Editor is loading...
Leave a Comment