🔒 Closed Need Help, Please po! Bukas ng 5pm na ang deadline!!!

Status
Not open for further replies.

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:


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
1617292950746.webp
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
 
yung value ng "n varialble" nagiging 2 kapag tumama sa first question kaya dina tinatawag yung question number 2 kasi dahil sa conditon mo sa question 2 na "n < 1 ". eh pano yan "2" na ang value ni "n".
 
under L.Question1(); lagay ka n = 0, d sya papasok sa L.Question2(); kasi ang while condition mo is n<=1 na napalitan na nung nag correct ung answer, or change mo ung while condition sa question to, n<=2 para may control variable ka sa number of question.
 
Tinatawag nya pa ang Question2, pero dahil nadagdagan na ang value ng "n" dahil sa pag-execute ng Question1 hindi na nya ma-execute ang while loop sa Question2 kasi nagfa-failed na ang condition na "n <= 1" dahil ang value na ng "n is 2" so ang nangyayari ay "2 <= 1" equals false kaya di na ma-execute ang looping doon sa Question2.
 
Ty, di ko napansin yung condition ko sa while cinopy and paste ko lang kase yung Question1 sa Question2 kaya di ko napalitan yung value ni while, salamat po
yung value ng "n varialble" nagiging 2 kapag tumama sa first question kaya dina tinatawag yung question number 2 kasi dahil sa conditon mo sa question 2 na "n < 1 ". eh pano yan "2" na ang value ni "n".

under L.Question1(); lagay ka n = 0, d sya papasok sa L.Question2(); kasi ang while condition mo is n<=1 na napalitan na nung nag correct ung answer, or change mo ung while condition sa question to, n<=2 para may control variable ka sa number of question.

Tinatawag nya pa ang Question2, pero dahil nadagdagan na ang value ng "n" dahil sa pag-execute ng Question1 hindi na nya ma-execute ang while loop sa Question2 kasi nagfa-failed na ang condition na "n <= 1" dahil ang value na ng "n is 2" so ang nangyayari ay "2 <= 1" equals false kaya di na ma-execute ang looping doon sa Question2.
 
Status
Not open for further replies.

About this Thread

  • 11
    Replies
  • 682
    Views
  • 6
    Participants
Last reply from:
Nero Swallowtail

Trending Topics

Online now

Members online
1,195
Guests online
4,388
Total visitors
5,583

Forum statistics

Threads
2,326,874
Posts
29,232,043
Members
1,164,551
Latest member
kskyouya07
Back
Top