r/programminghomework • u/RichmondMilitary • Sep 16 '17
[Java] Math Game
The general idea of the assignment is that two integers are randomly chosen, added together, and the user must put in the sum of the two numbers. This is repeated three times (three different equations to solve). If they input the answer incorrectly three times for one of the questions, they lose and the game ends.
import java.util.Scanner;
public class Homework{
public static void main(String[] args) { int varOne; int varTwo; int roundCount = 1; int gameCount = 1; int sum; int guess = 0;
do
{
varOne = (int) (Math.random()*11);
varTwo = (int) (Math.random()*11);
sum = varOne + varTwo;
Scanner in = new Scanner (System.in);
System.out.println ("What is " + varOne + " + " + varTwo + "?");
System.out.println ("Enter guess: ");
guess = in.nextInt();
if (guess == sum);
{
System.out.println("you win game #" + gameCount);
roundCount++;
break;
}while (roundCount <=3);
}This is what I have so far. I feel like I'm close but now I'm second guessing the use of the do-while loop and should've just used a while loop. Suggestions?
View all comments
Show parent comment
1
u/thediabloman Sep 17 '17
It sounds to me like you want to do the exact same thing, up to three times. That very much warrants a loop. Now when do you want to exit the loop? At one of two points: The user has answered correctly three times or the user has answered the current question incorrectly.
If you flip that logical statement around you will have your code for when you want to code to keep repeating.
The code you already have is very close, no reason to start over!