Skip to content
Advertisement

How do I get the numberOfQuestions variable to reset back to Zero after the loop completes?

/*Class MentalMathProgram

  • Michael
  • 11/18/2020
  • This program is designed to present the user with randomly generated numbers
  • and it gets progressively harder for every question correct. / import java.util.;

public class mentalMathProgram {

JavaScript

This is my code but I’m very new to coding. Im just trying to reset the variable that controls the amount of questions presented to the user to reset at the end of each loop. So far I’m unable to figure out what I’m doing wrong

Advertisement

Answer

There are a number of issues with your code that makes it difficult to trace / debug.

If I understand correctly, your outer doWhile is supposed to run indefinitely until user chooses to terminate the program.

The second doWhile is controlling the number of questions that are being asked in any single, complete round of the game.

Firstly, bring the ‘numberOfQuestions’ variable to be within the scope of the outer loop.

Secondly, you can simply declare a boolean variable to control whether the user wants to continue playing the game or to terminate the program.

Lastly, for each of the switch cases, you can simply do questionCounter++, instead of summing the correct and incorrect answers.

boolean keepGoing = true;

JavaScript

It is also good practice to make sure your lines are indented properly, so that you are able to see what codes belong in which block for better maintainability and debugging.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement