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
Wouldn't the player getting the correct answer be them staying in the loop?