/*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 {
static double ranNum(int min, int max){ Random ran = new Random(); double ranNum = min + (int)(Math.random() * ((max- - min)+ 1)); return (double)ranNum; } static byte mathType(int min, int max){ Random ran = new Random(); int mathType = min + (int)(Math.random() * ((max- - min)+ 1)); return (byte) mathType; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int restart = 0; int correctAnswers = 0,incorrectAnswers = 0; int numberOfQuestions = 0; byte mathType = 0; char again; again = 'Y'; while(again == 'Y') { do{ int questionCounter = 0; System.out.println(" nnWelcome to your mental math assistance program! There will" + "n be varying levels of difficulty as you progress through the questions" + "n or when you select the difficulty preset. " + "nn Please select a difficulty below!"); System.out.println("n 1. Easy" + "n 2. Normal" + "n 3. Medium" + "n 4. Hard"); byte difficultyChoice = input.nextByte(); switch(difficultyChoice){ case 1: { System.out.println("How many Questions do you want to do?"); numberOfQuestions = input.nextInt(); do { byte randomOperationMin = 1; byte randomOperationMax = 4; byte operationValue = mathType(randomOperationMin,randomOperationMax); mathType = operationValue; switch(mathType) { case 1: { System.out.println("nnn Easy difficulty Selected"); int easyMin = 1; int easyMax = 10; int result1=(int) ranNum(easyMin,easyMax); int result2=(int) ranNum(easyMin,easyMax); System.out.println("What is "+result1+ "+" +result2+ "="); int userAnswer = input.nextInt(); int answer = result1 + result2; if(userAnswer==answer) { System.out.println("Correct!"); correctAnswers++; } else { System.out.println("Incorrect! The Answer was "+answer); incorrectAnswers++; } questionCounter = correctAnswers + incorrectAnswers; break; } case 2: { System.out.println("nnn Easy difficulty Selected"); int easyMin = 1; int easyMax = 10; int result1=(int) ranNum(easyMin,easyMax); int result2=(int) ranNum(easyMin,easyMax); System.out.println("What is "+result1+ "-" +result2+ "="); int userAnswer = input.nextInt(); int answer = result1 - result2; if(userAnswer==answer) { System.out.println("Correct!"); correctAnswers++; } else { System.out.println("Incorrect! The Answer was "+answer); incorrectAnswers++; } questionCounter = correctAnswers + incorrectAnswers; break; } case 3: { System.out.println("nnn Easy difficulty Selected"); int easyMin = 1; int easyMax = 10; int result1=(int) ranNum(easyMin,easyMax); int result2=(int) ranNum(easyMin,easyMax); System.out.println("What is "+result1+ "*" +result2+ "="); int userAnswer = input.nextInt(); int answer = result1 * result2; if(userAnswer==answer) { System.out.println("Correct!"); correctAnswers++; } else { System.out.println("Incorrect! The Answer was "+answer); incorrectAnswers++; } questionCounter = correctAnswers + incorrectAnswers; break; } case 4: { System.out.println("nnn Easy difficulty Selected"); int easyMin = 1; int easyMax = 10; double result1=ranNum(easyMin,easyMax); double result2=ranNum(easyMin,easyMax); System.out.println("What is "+result1+ "/" +result2+ "="); double userAnswer = input.nextDouble(); double answer = result1 / result2; double remainder = result1 % result2; System.out.println("The Remainder is "+remainder); if(userAnswer==answer) { System.out.println("Correct!"); correctAnswers++; } else { System.out.println("Incorrect! The Answer was "+answer); incorrectAnswers++; } questionCounter = correctAnswers + incorrectAnswers; break; } } }while(questionCounter != numberOfQuestions); break; //I need to figure out a way to loop this code over and over instead of it just breaking out. I also need to // make it so that the user can exit the program whenever they want } } }while(restart==0);//condition for the do while death/restart loop System.out.println("nPlay Again? Y OR N: "); //println statement, asking if user would like to play again. System.out.println("Questions Correct: "+correctAnswers+""); again = input.nextLine().charAt(0); // set variable again to value assigned from user input } } }
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;
do { int numberOfQuestions = 0; int questionCounter = 0; System.out.println("How many questions?"); numberOfQuestions = sc.nextInt(); do { // ask questions repeatedly // for each case, questionCounter++ } while (questionCounter != numberOfQuestions); System.out.println("Enter '0' to continue"); if (input.nextInt() != 0) { keepGoing = false; } } while (keepGoing);
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.