0

my program is supposed to take questions from a file and print them and the answers. then the user is supposed to answer them and if they get it wrong the program is supposed to deduct points. each question is worth 5 and getting it wrong makes you lose 2. But when you get it wrong its supposed to keep asking you the question not just go on to the next how can i fix this? And show the user how many points they have left.

import java.util.Scanner;

public class HW21 { 
  public static void main(String[] args) {     
    Scanner input = new Scanner(System.in);
    HWData hd = new HWData();
  int currentScore = 5;

int numberOfQuestions = hd.getNumberOfQuestions();
for (int questionIndex=0; questionIndex<numberOfQuestions; ++questionIndex) {
    System.out.println(hd.getQuestion(questionIndex));
    System.out.println();
    System.out.println("1. " + hd.getAnswer1(questionIndex));
    System.out.println();
    System.out.println("2. " + hd.getAnswer2(questionIndex));
    System.out.println();
    System.out.println("3. " + hd.getAnswer3(questionIndex));
    System.out.println();
    System.out.println("4. " + hd.getAnswer4(questionIndex));

    int answer = input.nextInt();

    if (answer == hd.getCorrect(questionIndex)) {
        System.out.println(" Great Job! You got the right answer!");
    }
    else {
        System.out.println(" You got the answer wrong.");
        currentScore -= 2;
    }
}

input.close();
System.out.println("Final score: " + currentScore);

  }

  }
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93

2 Answers2

0

Another solution could be to reduce the questionIndex by one so that the loop stays in the same place until the user gets it right. something like this:

    import java.util.Scanner;

public class HW21 { 
    public static void main(String[] args) {     
        Scanner input = new Scanner(System.in);
        HWData hd = new HWData();
        int currentScore = 5;

        int numberOfQuestions = hd.getNumberOfQuestions();
        for (int questionIndex=0; questionIndex<numberOfQuestions; ++questionIndex) {
            System.out.println(hd.getQuestion(questionIndex));
            System.out.println();
            System.out.println("1. " + hd.getAnswer1(questionIndex));
            System.out.println();
            System.out.println("2. " + hd.getAnswer2(questionIndex));
            System.out.println();
            System.out.println("3. " + hd.getAnswer3(questionIndex));
            System.out.println();
            System.out.println("4. " + hd.getAnswer4(questionIndex));

            int answer = input.nextInt();

            if (answer == hd.getCorrect(questionIndex)) {
                System.out.println(" Great Job! You got the right answer!");
            }
            else {
                System.out.println(" You got the answer wrong.");
                qestionIndex--;
                currentScore -= 2;
            }
        }

        input.close();
        System.out.println("Final score: " + currentScore);

    }

}
  • This code is bad practice , refer http://stackoverflow.com/questions/30088401/change-index-inside-a-for-loop . – CyprUS Oct 23 '16 at 00:57
0

I am giving some pseudo code for you to work through . It sounds like a homework assignment to me, hence I will refrain from posting the full code here.

1. Get number of questions 2. Initialize totalScore to 5 // I am taking this from your code snippet 2. For all questions, repeat steps 3. Ask Question i 4. if answer(i) == correct totalScore = totalScore + 5 Move on to next question Else, totalScore = totalScore -2 Print totalScore Go back to Step 3 5. At this step, the loop is done. Print totalScore or whatever actions you need to do

Yes, the Go back part will require the use of some flag variable that indicates if answer is correct or not.

I hope you can work it out now. By the way, by and large your code seems okay. You are on the right path.

CyprUS
  • 4,159
  • 9
  • 48
  • 93