What's new

Pa Help po ulet sa java ko please!!

Nero Swallowtail

Eternal Poster
Joined
Aug 13, 2020
Posts
465
Reaction
174
Points
280
nagawan ko ng ng exception ung pag hindi a b or c ung ininput is mag re resort sya sa exception, kaso ang problema naman ngayon is sinasalo nya yung conditon ni Blank Exception at Special Character Exception, ano po bang Conditon ang dapat kong ilagay? eto po ung code!
 
Code:
import java.util.*;

public class LabExer5B {
    static Scanner scan = new Scanner(System.in);
    static String [] Question = {"Which is the best Muzzle when controlling your recoil?", "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";
    static int n = 1, CorrectAns = 0, QuestionsLeft = 10, attemptLeft = 3;
    
    
    public static void main(String[] args) {
        LabExer5B L = new LabExer5B();
        L.Question1();
        L.Question2();
    
        }

    
    public void Question1() {
        while(n <=3) {
        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 += 3;
            System.out.println("You are Correct! \nScore: " + CorrectAns + "\n" + QuestionsLeft + " Question(s) Left");
            
            
            
        }
        
        else if(!UserAns.equalsIgnoreCase(Answer1) && UserAns.equalsIgnoreCase("a") || UserAns.equalsIgnoreCase("b") || UserAns.equalsIgnoreCase("c")) {
            QuestionsLeft--;
            n+=3;
            System.out.println("You are wrong!" +"\nScore: "+ CorrectAns + "\nQuestion(s) Left: " + QuestionsLeft);
            continue;
                    
        }
        if(!UserAns.equalsIgnoreCase("a") && !UserAns.equalsIgnoreCase("b") && !UserAns.equalsIgnoreCase("c")) {
            throw new InvalidLetterException();
            
        
            
        }
        if(UserAns.isBlank() && UserAns.isEmpty() &&  UserAns == null) {
            
            throw new EmptyStringException();
            
        }
        
        
        }
            
    catch (InvalidLetterException ex) {
        attemptLeft--;
        
        System.out.println(ex.getMessage());
        
        n++;
        
    }
        
    catch(EmptyStringException ex){
    System.out.println(ex.getMessage());   
    }
  }   
}
    public void Question2() {
        System.out.println(Question[1]);
        System.out.println(Choices[0]);
        System.out.print(prompt);
        UserAns = scan.nextLine();
    }
    class InvalidLetterException extends Exception{
        public InvalidLetterException(){
            super("Invalid Letter! \nPlease Enter a Valid Answer! \nYou only have " + attemptLeft + " attempt(s) Left");
        }
        }
    class EmptyStringException extends Exception {
        public EmptyStringException() {
            super("Answer Cannot be Empty!");
        }
    }
    class SpecialCharacterException extends Exception {
        public SpecialCharacterException() {
            super("Your Answer Cannot be Like That!");
        }
    }
    

}
 
[XX='Nero Swallowtail, c: 1021292, m: 1752019'][/XX] Sayo din ata ung isa pak icheck naman kung tama un output. Pki like na lng kung ok na.
 
Back
Top