Home > Software design >  how to let the user answer same question if input is invalid in a quiz
how to let the user answer same question if input is invalid in a quiz

Time:10-27

I'm making a quiz-game for my school project. I'm having trouble to let the user answer the same question if the input is invalid. I want to let the user answer the same question when no valid input on all questions.

here's my code so far :

import java.util.Scanner;

public class MainClass {

    public static void main(String[]args) {
    
        String quest1 = ("Who founded the company spaceX?\na: Will Smith\nb: Elon Musk\nc: Kevin Hart");
        String quest2 = ("Which sport is the most famous in the world?\na: Football\nb: Basketball\nc: Tennis");
        
        questionsAndAnswers [] questionAnswers = {          
                new questionsAndAnswers(quest1, "b"),
                new questionsAndAnswers(quest2, "a")
        };
    
        doQuiz(questionAnswers);
    }

    public static void doQuiz(questionsAndAnswers[] questionAnswers) {
        
        int points = 0;
        
        Scanner input = new Scanner(System.in);
        
        boolean looper = true;
        
        while (looper) {
            
            for(int i = 0; i < questionAnswers.length; i  ){
                System.out.println(questionAnswers[i].questions);
            
                String userAnswer = input.nextLine();
    
                if(!"a".equals(userAnswer) && !"b".equals(userAnswer) && !"c".equals(userAnswer)) {
                    
                    System.out.println("Invalid input, try again");
                    break;
                }
                
                else if(userAnswer.equals(questionAnswers[i].answers)) {            
                    points  ;
                    System.out.println("Correct! Your points:"   points);
                }
                continue;
            }
            looper = false;
        }
    }   
}

CodePudding user response:

the code is great so far. When a user answers a question with an invalid answer you already detect that which is great however what you want to do is re ask the user for input so if you keep track of if a user has entered a valid or not

boolean hasEnteredValidAnswer = false
while(!hasEnteredValidAnswer) {
    String userAnswer = input.nextLine();

    if("a".equals(userAnswer) || "b".equals(userAnswer) || "c".equals(userAnswer)) {
        hasEnteredValidAnswer = true                    
    }
    else {
        System.out.println("Invalid input, try again");
    }
}

CodePudding user response:

Use a while loop INSTEAD of a for loop:

        int i = 0;
        while (i < questionAnswers.length){
            System.out.println(questionAnswers[i].questions);
        
            String userAnswer = input.nextLine();

            if(userAnswer.equals(questionAnswers[i].answers)) {            
                points  ;
                System.out.println("Correct! Your points:"   points);
                  i;
            } else {
                System.out.println("Invalid input, try again");
                // do not increment i
            }
        }
  • Related