Nero Swallowtail
Honorary Poster
nag s stop po ung program ko kapag tama ung ininput ni user! alam nyo po ba kung pano ayusin?
Code:
Thanks po sa tutulong
Edit:
ganto po ung output

okay po ung output nya kaso nag te terminate sya kapag tama yung sagot mo sa unang question, di sya nag po proceed sa next question
Code:
JavaScript:
import java.util.*;
public class LabExer5B {
static Scanner scan = new Scanner(System.in);
static String [] Question = {"Which is the BEST Muzzle when you want to reduce the recoil of your Gun?", "Which is the best Muzzle to reduce the sound of a Gun?"};
static String [] Choices = {"a. Suppressor \nb. Compensator \nc. Flash Hider", "a. Flash Hider \nb. Compensator \nc. Suppresor" };
static String prompt = "Enter your Answer here: ";
static String UserAns;
private static String Answer1 = "b", Answer2 = "a";
static int n = 0, CorrectAns = 0, QuestionsLeft = 10;
private static String SpecialCharacter = "~`!@#$%^&*()_-+= {}[]\\|;:'<>,.?/";
private static String Intergers ="1234567890";
public static void main(String[] args) {
LabExer5B L = new LabExer5B();
L.Question1();
L.Question2();
}
public void Question1() {
while(n <=1) {
System.out.println(Question[0]);
System.out.println(Choices[0]);
System.out.print(prompt);
UserAns = scan.nextLine();
try {
if(UserAns.equalsIgnoreCase(Answer1)) {
CorrectAns = CorrectAns + 1;
QuestionsLeft--;
n += 2;
System.out.println("You are Correct! \nScore: " + CorrectAns + "\n" + QuestionsLeft + " Question(s) Left\n");
}
else if(!UserAns.equalsIgnoreCase(Answer1) && UserAns.equalsIgnoreCase("a") || UserAns.equalsIgnoreCase("b") || UserAns.equalsIgnoreCase("c")) {
QuestionsLeft--;
n+=2;
System.out.println("You are wrong!" +"\nScore: "+ CorrectAns + "\nQuestion(s) Left: " + QuestionsLeft + "\n");
continue;
}
else if(UserAns.isBlank() || UserAns.isEmpty()) {
throw new EmptyStringException();
}
else if(SpecialCharacter.contains(UserAns) || Intergers.contains(UserAns)) {
throw new SpecialCharacterException();
}
else if(!UserAns.equalsIgnoreCase("a") || !UserAns.equalsIgnoreCase("b") ||!UserAns.equalsIgnoreCase("c")) {
throw new InvalidLetterException();
}
}
catch (InvalidLetterException ex) {
System.out.println(ex.getMessage());
}
catch(EmptyStringException ex){
System.out.println(ex.getMessage());
}
catch(SpecialCharacterException ex) {
System.out.println(ex.getMessage());
}
}
}
public void Question2() {
while(n <=1) {
System.out.println(Question[1]);
System.out.println(Choices[1]);
System.out.print(prompt);
UserAns = scan.nextLine();
try {
if(UserAns.equalsIgnoreCase(Answer2)) {
CorrectAns = CorrectAns + 1;
QuestionsLeft--;
n += 1;
System.out.println("You are Correct! \nScore: " + CorrectAns + "\n" + QuestionsLeft + " Question(s) Left\n");
continue;
}
else if(!UserAns.equalsIgnoreCase(Answer2) && UserAns.equalsIgnoreCase("a") || UserAns.equalsIgnoreCase("b") || UserAns.equalsIgnoreCase("c")) {
QuestionsLeft--;
n+=1;
System.out.println("You are wrong!" +"\nScore: "+ CorrectAns + "\nQuestion(s) Left: " + QuestionsLeft + "\n");
continue;
}
else if(UserAns.isBlank() || UserAns.isEmpty()) {
throw new EmptyStringException();
}
else if(SpecialCharacter.contains(UserAns) || Intergers.contains(UserAns)) {
throw new SpecialCharacterException();
}
else if(!UserAns.equalsIgnoreCase("a") || !UserAns.equalsIgnoreCase("b") ||!UserAns.equalsIgnoreCase("c")) {
throw new InvalidLetterException();
}
}
catch (InvalidLetterException ex) {
System.out.println(ex.getMessage());
}
catch(EmptyStringException ex){
System.out.println(ex.getMessage());
}
catch(SpecialCharacterException ex) {
System.out.println(ex.getMessage());
}
}
}
}
class InvalidLetterException extends Exception{
public InvalidLetterException(){
super("Invalid Letter! \nPlease Enter a Valid Answer! \nTry Again! \n");
}
}
class EmptyStringException extends Exception {
public EmptyStringException() {
super("Answer Cannot be Empty! \nPlease Enter a Valid Answer! \nTry Again!\n");
}
}
class SpecialCharacterException extends Exception {
public SpecialCharacterException() {
super("Your Answer Cannot be Like That! \nPlease Enter a Valid Answer! \nTry Again!\n");
}
}
Thanks po sa tutulong
Edit:
ganto po ung output

okay po ung output nya kaso nag te terminate sya kapag tama yung sagot mo sa unang question, di sya nag po proceed sa next question

