1

I created a quiz program that keeps track of students score. What I am trying to do is if the student received 100% then they get a message that their score is 100%. If the the score is less than 100 then the program should restart and keep the counter of up to 5 tries in the counter integer.

Once the counter reaches int of 5 with the score of less than 3 than break the program and display message "take a quiz later"

What is working right now: I am able to keep track of the "score" int variable and its working if you get 100% or less than 100%.

What I am trying to get working: Get the "counter" int variable working to keep the record of the number of tries so the user gets up to 5 tries and restart the whole console program while maintaining the score on the "counter" variable. for instance:

counter < 5 - try again 
     counter++
counter >= 5 - end the program.

Here is the end of the program. Maybe I should somehow place it in the method and recall it in my public void run but I was unable to accomplish that and keep record of scores. I have many loops so it would be unrealistic to write the whole program in loop one big loop.

Thanks!

   public void run()
    {
        if (score >= 3) 
            {
            println("You have passed the exam with 100%");
            }   
                else if (counter<5) 
                {
                counter++;
                println("You're score is less than 100%.");
                println(" ");
                println("Try Again!");
                //restart the questions until you're out of 5 attempts
                } 
                    else if (counter==5)
                    {
                        println("You're out of your 5 attempts");
                    }
    }
Dmitriy
  • 97
  • 3
  • 15

2 Answers2

0

If you are trying to achieve persistence of data after a program has finished then the standard way of doing this would be to write this to a file. It seems you would want to keep track of each individual student's score and count. You could either save a separate file for each student or save all your data in one big file. Useful file formats could be XML, JSON or YAML. I've never used it but you may also wish to explore the Java Preferences API.

Rem-D
  • 627
  • 6
  • 15
0

I think you want something like this. Try it - Create a class and try this code(This is just a demo. You may increase the number of questions and the scoring pattern according to your choice. Subsequently you may need to modify the code) -

public void display() {
    int counter = 1;
    List<String> list = new LinkedList<String>();
    int score = checkAnswers(list);
    if (score == 2) {
        System.out.println("Your score is 100% in 1st attempt");
    } else {
        while (counter <= 5) {
            counter++;
            int newScore=checkAnswers(list);
            if(newScore==2){
                System.out.println("Your score is 100% in "+counter+" attempts");
                break;
            }
            if(counter==5){
                System.out.println("You have finished your 5 attempts. Please take the quiz later.");
                break;
            }
        }
    }


}

public List<String> questions() {
    List<String> list = new LinkedList<String>();
    Scanner scan = new Scanner(System.in);
    System.out.println("type your 1st question ");
    list.add(scan.nextLine());
    System.out.println("type your 2nd question: ");
    list.add(scan.nextLine());


    return list;
}

public int checkAnswers(List<String> list) {
    int score = 0;
    list = questions();
    List<String> answerList = new LinkedList<String>();
    answerList.add("type answer of 1st question");
    answerList.add("type answer of 2nd question");
    for (int i = 0; i < list.size(); i++) {
        for (int j = 0; j < answerList.size(); j++) {
            if (list.get(i).equals(answerList.get(j))) {
                score++;
            }
        }
    }
    return score;
}

Now in another class declare the main method and inside it just create an object of this class and call the display() method only. Hope this helps! :)

Aritro Sen
  • 357
  • 7
  • 14