d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Flash Cards Program Help Me
Add Reply New Topic New Poll
Member
Posts: 7,707
Joined: Jun 28 2007
Gold: 3,358.57
Feb 14 2014 05:37pm
I have to write this program that provides 2 random numbers between 0 and 99 and asks the user whether they would like to solve an addition or subtraction problem. (Default to addition if they fail to enter 0 or 1 two times)

Then prompt the user to answer the problem using the 2 random numbers. If the answer is correct, ask them to try again, if they fail again, provide correct answer.

I'm sure this is a very basic program for any of you but I can't seem to get it. We're supposed to use while/if/else if statements.

I've started it but can't fill in the rest that I need. Willing to pay for help to finish this. (Trying to go out with a girl tonight but I must complete this first).

Very incomplete but here's what I have started (with some comments from my professor):

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 + " " + num2); //I suppose this is a debug statement???

// + operator with two integers will act just like a math operator (3 + 5 = 8)
//You have to enter a "whitespace" string to let the compile know you want the actual
//values of num1 and num2 to be printed else you'll get their sum.

//There is a difference between System.out.print and System.out.println
//System.out.print does not terminate the line - the line continues until the user enters the carriage return (e.g. enter / return)
//System.out.println terminates the line - meaning the println injects the carriage return for you.

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 answer = input.nextInt();

int count = 0;
while(answer != 0 && answer != 1 || count > 2)
{
System.out.println("Sorry, you entered the wrong number (please enter 0 for addition or 1 for subtraction): ");
answer = input.nextInt();
count++;
}
}
}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 14 2014 05:47pm
Looks like you are off to a good start. Your while loops condition is wrong. You want to prompt for input until the user has tried twice. You are tracking how many times the user has attempted with the count variable. This means you would want to loop until count < 2, since you are starting at 0.

What else are you stuck on?
Member
Posts: 7,707
Joined: Jun 28 2007
Gold: 3,358.57
Feb 14 2014 05:59pm
Quote (Minkomonster @ Feb 14 2014 07:47pm)
Looks like you are off to a good start. Your while loops condition is wrong. You want to prompt for input until the user has tried twice. You are tracking how many times the user has attempted with the count variable. This means you would want to loop until count < 2, since you are starting at 0.

What else are you stuck on?


Oh wow I never would have noticed I used a > sign haha...

Well I'm confused as to how to finish out the program. I suppose I just need if statements in order to do the next few parts but I'm unsure how to set it up correctly.

I just tried running it, and when I enter 0 or 1, it prints "Sorry, you entered the wrong number etc." is that because I'm missing the if statements where the user enters 0 or 1?

This post was edited by SkaDiablo on Feb 14 2014 06:01pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 14 2014 06:02pm
Something like this perhaps?

Code
if(answer == 0)
{
//do stuff for addition
}
else if(answer == 1)
{
//do stuff for subtraction
}
Member
Posts: 7,707
Joined: Jun 28 2007
Gold: 3,358.57
Feb 14 2014 06:04pm
Quote (Minkomonster @ Feb 14 2014 08:02pm)
Something like this perhaps?

Code
if(answer == 0)
{
  //do stuff for addition
}
else if(answer == 1)
{
  //do stuff for subtraction
}


Okay that's about what I was going to do. So on top of that, how do I set up whether they answer right or wrong, and prompt accordingly?

I got it to print out an addition problem if the user enters 0, and allows them to make a guess, after guess is entered, it still prints "Sorry you entered the wrong number etc."

This post was edited by SkaDiablo on Feb 14 2014 06:11pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Feb 14 2014 06:20pm
Follow what the while loop did for obtaining user input. You could do something like

Code


int correctAnswer;

if(user selected addition)
{
correctAnswer = add the 2 random numbers;
}
else if(user selected subtraction)
{
correctAnswer = subtract the 2 random numbers;
}

while(user still has attempts and answers differ)
{
int answer = user input;

tally an attempt
}


This post was edited by Minkomonster on Feb 14 2014 06:20pm
Member
Posts: 7,707
Joined: Jun 28 2007
Gold: 3,358.57
Feb 14 2014 06:34pm
Update on what I have. (provides addition problem if user enters 0 and if they answer correctly, prints out congratulations. Still prints out addition problem if user enters 1, however, and after the problem is completed, continues to print out "Sorry you've entered the wrong number"

Code
import java.util.Random;
import java.util.Scanner;

public class FlashCards
{
public static void main (String[] args)
{

int actualanswer;
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 answer = input.nextInt();

int correctAnswer = num1 + num2;
if (answer == 0); {
System.out.println("Ok, " + num1 + " + " + num2 + " = ");
int guess = input.nextInt();
if (guess == correctAnswer); {
System.out.println("Congratulations, you got it right! " + num1 + " + " + num2 + " = " + guess);
}
}
if (answer == 1); {
System.out.println("Ok, " + num1 + " - " + num2 + " = ");
int guess = input.nextInt();
}


int count = 0;
while(answer != 0 && answer != 1 || count < 2)
{
System.out.println("Sorry, you entered the wrong number (please enter 0 for addition or 1 for subtraction): ");
answer = input.nextInt();
count++;
}
}
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll