jeudi 29 octobre 2015

Java loop run only for the max tries

I had created an if statement while its true to keep on asking question until you get it right. However that is not an option in my case. I have implemented a counter with the max tries, if the loop runs 3 times and you still got it wrong then print "you got it wrong" and go to the next question. If they get it right within 3 tries, "go to the next question" the score++ is just there to keep track of the score. score++ must keep the score only for the correct answer that is why its under while (true). If someone could help me out, that would be awesome. I am open to different ways I can improve my loop. Thank you!

Here's the loop:

int count = 0;
int maxTries = 3;

do {
    println(q1.PrintQuestion());
    yourAnswer = readLine("Your answer: ");
    if ((q1.IsAnswerCorrect(yourAnswer)) && (count < maxTries))
        break;
    else
        println("You got it wrong! \n");

} while (true);
count++;
score++; 
println("You got it right! \n");

Aucun commentaire:

Enregistrer un commentaire