d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Why Won't This Compile?
Add Reply New Topic New Poll
Member
Posts: 7,707
Joined: Jun 28 2007
Gold: 3,358.57
Feb 14 2014 09:40pm
Code
import java.util.Random;
import java.util.Scanner;

public class FlashCards_SC
{
public static void main (String[] args)
{
Random generator = new Random();

int num1 = generator.nextInt(100);
int num2 = generator.nextInt(100);
System.out.println(num1 + " and " + num2);

Scanner input = new Scanner(System.in);

System.out.println("Which type of problem would you like to solve (please enter 0 for addition or 1 for subtraction): ");

int selection = input.nextInt();

int count = 0;
while(selection != 0 && selection != 1 && count < 2)
{
System.out.println("Sorry, you entered the wrong number (please enter 0 for addition or 1 for subtraction): ");
selection = input.nextInt();
count++;

if(count >= 2)
selection = 0; //Default case when user doesn't enter a valid input after two tries.
}

int correctAnswer = 0;
int guess = 0;

if (selection == 0)
{
correctAnswer = num1 + num2;
System.out.println("Ok, " + num1 + " + " + num2 + " = ");
guess = input.nextInt();

while (guess != correctAnswer)
{
System.out.println("Sorry, " + num1 + " + " + num2 + " is not " + guess + ", please try again. " + num1 + " + " + num2 + " = ");
guess = input.nextInt();
count++;

if (count >= 2)
System.out.println("Sorry, " + num1 + " + " + num2 + " is not " + guess + ". The correct answer is " + num1 + " + " + num2 + " = " + correctAnswer);
}

//Above is a second while loop to allow the user to make more than one guess when they are wrong.

if (guess == correctAnswer)
{
System.out.println("Congratulations, you got it right! " + num1 + " + " + num2 + " = " + guess);
}
}

int correctAnswer = 0;
int guess = 0;

if (selection == 1)
{
correctAnswer = num1 - num2;
System.out.println("Ok, " + num1 + " - " + num2 + " = ");
guess = input.nextInt();

while (guess != correctAnswer)
{
System.out.println("Sorry, " + num1 + " - " + num2 + " is not " + guess + ", please try again. " + num1 + " - " + num2 + " = ");
guess = input.nextInt();
count++;

if (count >= 2)
System.out.print("Sorry, " + num1 + " - " + num2 + " is not " + guess + ". The correct answer is " + num1 + " - " + num2 " = " + correctAnswer);
}

//Above is a second while loop to allow the user to make more than one guess when they are wrong.

if (guess == correctAnswer)
{
System.out.println("Congratulations, you got it right! " + num1 + " - " + num2 + " = " + guess);
}
}
}
}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Feb 14 2014 09:41pm
you realize there's a program called a "compiler" that tells you why it's not compiling, right?

if you don't understand the output from the compiler, then post the output and highlight the line.

what you should NOT do is blindly post a bunch of code and ask us why it doesn't compile.

This post was edited by carteblanche on Feb 14 2014 09:43pm
Member
Posts: 7,707
Joined: Jun 28 2007
Gold: 3,358.57
Feb 14 2014 09:42pm
Quote (carteblanche @ Feb 14 2014 11:41pm)
you realize there's a program called a "compiler" that tells you why it's not compiling, right?


I did not realize.

Quote (carteblanche @ Feb 14 2014 11:41pm)
you realize there's a program called a "compiler" that tells you why it's not compiling, right?

if you don't understand the output from the compiler, then post the output and highlight the line.

what you should NOT do is blindly post a bunch of code and ask us why it doesn't compile.


Thank you for the advice, I'm a beginner, I don't know what I'm doing

This post was edited by SkaDiablo on Feb 14 2014 09:49pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 14 2014 10:51pm
Just glancing over it, in your last while loop, there is a print statement that you messed up.

Code
System.out.print("Sorry, " + num1 + " - " + num2 + " is not " + guess + ". The correct answer is " + num1 + " - " + num2 " = " + correctAnswer);


should be

Code
System.out.print("Sorry, " + num1 + " - " + num2 + " is not " + guess + ". The correct answer is " + num1 + " - " + num2 + " = " + correctAnswer);


It also looks like you have the variables correctAnswer and guess declared twice within the same scope.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll