Reputation: 1
Basically the last thing I need to do for this Math Quiz I have to program, I have to ask the user if they would like to answer more problems, if yes, rerun everything in the Main Method. If no, print goodbye. The no is easy, but I'm unsure how to tell it to rerun the main method if they say yes. Here is the code in my main method.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int digit = 0;
String result1 = getUserChoice("");
digit = getNumberofDigit1(digit);
int numberOfProblems = amountOfProblems();
for (int i = 0; i < numberOfProblems; i++) {
int number1 = getRandomNumber1(digit);
int number2 = getRandomNumber2(digit);
System.out.println("Enter your answer to the following problem: \n" +
number1 + result1 + number2);
int correctAnswer = getCorrectAnswer(number1, result1, number2);
int userAnswer = getUserAnswer();
CheckandDisplayResult(correctAnswer, userAnswer);
}
System.out.println("Would you like to solve more probelms(Y/N)? ");
String moreProblems = in.next();
if ("Y".equals(moreProblems)){
digit = 0;
result1 = getUserChoice("");
digit = getNumberofDigit1(digit);
numberOfProblems = amountOfProblems();
for (int i = 0; i < numberOfProblems; i++) {
int number1 = getRandomNumber1(digit);
int number2 = getRandomNumber2(digit);
System.out.println("Enter your answer to the following problem: \n" +
number1 + result1 + number2);
int correctAnswer = getCorrectAnswer(number1, result1, number2);
int userAnswer = getUserAnswer();
CheckandDisplayResult(correctAnswer, userAnswer);
}
System.out.println("Would you like to solve more probelms(Y/N)? ");
moreProblems = in.next();
if ("Y".equals(moreProblems)){
}
System.out.println("Thank you for taking this quiz, Goodbye!");
}
Now I have tried something like,
if "Y".equals(moreProblems)){ copy and past the main method }
But that has the error of requiring an infinite loops as you'd have to have the more problems statement in every if of yes, meaning it would never end coding wise, you would keep copying and pasting forever.
Upvotes: 0
Views: 19118
Reputation: 2072
I guess this prototype might help you
import java.util.Scanner;
public class MathsClass {
public static void main(String[] args){
MathsClass object = new MathsClass();
while(object.response())
object.mathsQuiz();
}
public void mathsQuiz(){
//your quiz functionalities
System.out.println("Add two nos");
}
public boolean response(){
System.out.println("Would u like to solve more problems? ");
Scanner scanner = new Scanner(System.in);
boolean response = scanner.nextBoolean();
return response;
}
}
Upvotes: 1
Reputation: 37845
Put all of it in a big do-while loop:
boolean more = false;
do {
// all your code
more = "Y".equals(moreProblems);
} while (more);
Or provided your Scanner is declared outside the loop you can just:
do {
// all your code
} while ("Y".equals(in.next()));
Upvotes: 1
Reputation: 62052
Alternative to what others have suggested, this is the method I prefer:
while(true) {
//do all your stuff
if(/*some exit condition*/) { break; }
}
Upvotes: 2
Reputation: 34146
You could enclose all the code you want to "re-run" in a while loop:
boolean run = true;
while (run) {
// Here your code
// Here input if user want to re-run
if (getUserChoice("").equals("NO"))
run = false;
}
Upvotes: 3
Reputation: 81684
What you can do is move everything in main()
into another static method, call it interact()
. Then in main()
, just have logic which calls interact()
as long as the user wants to interact with your program. In other words, put the math quiz into one method, and the business of presenting the quiz into main()
. Your program will be easier to read and easier to modify further, if needed.
Upvotes: 1