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/RichmondMilitary Sep 17 '17
We'll see that's where I don't know. In my mind I need three while loops with each one being coded for each game round. And then creating a condition where if their input (guess) does not equal the correct answer (sum) then they'd stay and loop unless their number of guesses is at 3 then it'll kick them out and tell them they lost