# Random maths operator generation

• April 7th, 2013, 03:55 PM
dk87
Random maths operator generation
Good evening. :)

I am writing a program which allows the user to input their name and then answer ten questions that take the form "1 + 2 = ?", where the ? is, of course, the answer to be given. The numbers and operator - add or subtract - are to be generated at random. I have almost completed the program, but the real stumbling block right now is knowing what to do for the random operator; I have tried using a switch statement, but that didn't work for some reason (although whether I did it right or not is another matter entirely), and I don't know what else to try.

Below is what I've done so far:

Code :

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

public class MathsQuestions {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String user;

user = scan.nextLine();
System.out.println("Hello " + user + ", let's begin.");
playGame();
System.out.println("Thank you for playing; hope to see you again soon!");
}

public static void playGame() {
Random rand = new Random();
int count = 1;
int score = 0;
int firstNumber = rand.nextInt(8) + 11;
int secondNumber = rand.nextInt(9) + 1;
char operator = rand.nextBoolean() ? '+' : '-';

while(count <= 10) {
System.out.print(count + ".   " + firstNumber + operator + secondNumber + " = ");
System.out.println("Correct!");
count++;
score++;
} else {
count++;
}
}

if (score == 10) {
System.out.println("You scored " + score + " - excellent!");
} else if (score > 5) {
System.out.println("You scored " + score + " - good job.");
} else if (score > 1) {
System.out.println("You scored " + score + " - you need to try harder.");
} else {
System.out.println("You scored " + score + " - it'd be worth getting extra help.");
}
}
}

I would appreciate any help I can get, as this is becoming quite frustrating...

Regards. :)>-
• April 7th, 2013, 04:06 PM
Norm
Re: Random maths operator generation
Quote:

what to do for the random operator; I have tried using a switch statement, but that didn't work for some reason
What is wrong with the posted code? What happens when it is executed? What does it do or not do correctly?
• April 7th, 2013, 04:27 PM
dk87
Re: Random maths operator generation
Quote:

Originally Posted by Norm
What is wrong with the posted code? What happens when it is executed? What does it do or not do correctly?

The switch statement I tried (that is not in my code now) was as follows:

Code :

int num = (int)(2*Math.random()+1);
char operator;
// ...
switch (num) {
case 1: operator = '+';
break;
case 2: operator = '-';
break;
}

...but I got three instances of the "cannot find symbol" error whenever I tried to compile the program.

As for the code in my first post, it compiles and runs correctly but neither the numbers nor the operator change from question to question; they stay the same for all ten questions, and only change when I run the program again.
• April 7th, 2013, 04:41 PM
Norm
Re: Random maths operator generation
Can you post the contents of the error messages that show the cannot find symbol error?

Quote:

neither the numbers nor the operator change from question to question
And the contents of the console when you execute the current program.

How can you execute the code in post #1? It does not compile without errors.
• April 7th, 2013, 05:03 PM
dk87
Re: Random maths operator generation
Quote:

Originally Posted by Norm
How can you execute the code in post #1? It does not compile without errors.

My mistake; I just remembered that it executed well if I used a single, fixed operator rather than the random operator statement (although there was still no change in the numbers)... :o

The code, as it stands, is as follows:

Code :

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

public class MathsQuestions {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String user;

user = scan.nextLine();
System.out.println("Hello " + user + ", let's begin.");
playGame();
System.out.println("Thank you for playing; hope to see you again soon!");
}

public static void playGame() {
Random rand = new Random();
int count = 1;
int score = 0;
int firstNumber = rand.nextInt(8) + 11;
int secondNumber = rand.nextInt(9) + 1;
int num = (int)(2*Math.random()+1);

switch (num) {
case 1: operator = '+';
break;
case 2: operator = '-';
break;
}

while(count <= 10) {
System.out.print(count + ".   " + firstNumber + num + secondNumber + " = ");
System.out.println("Correct!");
count++;
score++;
} else {
count++;
}
}

if (score == 10) {
System.out.println("You scored " + score + " - excellent!");
} else if (score > 5) {
System.out.println("You scored " + score + " - good job.");
} else if (score > 1) {
System.out.println("You scored " + score + " - you need to try harder.");
} else {
System.out.println("You scored " + score + " - it'd be worth getting extra help.");
}
}
}

...and the contents of the error messages when I execute it (I'm doing this through the Command Prompt):

Code :

MathsQuestions.java:28: error: cannot find symbol
case 1: operator = '+';
symbol: variable operator
location: class MathsQuestions

MathsQuestions.java:30: error: cannot find symbol
case 1: operator = '-';
symbol: variable operator
location: class MathsQuestions

• April 7th, 2013, 05:15 PM
Norm
Re: Random maths operator generation
The compiler can not find a definition for the symbol named in the error message. Be sure to declare/define all variables used in the program.

Quote:

no change in the numbers
Where do the numbers get changed? Where are the contents of the numbers displayed? Are the contents of the numbers changed between the times they are shown? If the code does not change them, their contents will remain the same.
• April 7th, 2013, 06:08 PM
dk87
Re: Random maths operator generation
Quote:

Originally Posted by Norm
Are the contents of the numbers changed between the times they are shown? If the code does not change them, their contents will remain the same.

The range of the first number is 11 to 19, while the range of the second number is 1 to 9. As far as I can see I have followed the syntax for declaring variables for random numbers to the tee, yet for some reason the number stays the same throughout a single execution... for example, this little piece of code (written separately, just to make sure that I have indeed gotten the syntax right):

Code :

import java.util.Random;

public class Maths {
public static void main(String[] args) {
Random random = new Random();
int randomNumber = random.nextInt(8)+11;

for (int count = 1; count <= 10; count++) {
System.out.println(randomNumber);
}
}
}

...produces the following output when I run it once:

Code :

13
13
13
13
13
13
13
13
13
13

...and then this when I run it a second time:

Code :

18
18
18
18
18
18
18
18
18
18

Either I'm being stupid or it's a simple case of oversight, but I just can't get my head around this one... :mad:
• April 7th, 2013, 06:21 PM
Norm
Re: Random maths operator generation
Where is the number given a value?
Where is the number displayed?
Is there a loop?
Are both of the above locations INSIDE the same loop?

If you do not change the number's value, its value will remain the same.
• April 7th, 2013, 06:51 PM
dk87
Re: Random maths operator generation
Quote:

Originally Posted by Norm
Are both of the above locations INSIDE the same loop?

If you do not change the number's value, its value will remain the same.

And that's where I've been going wrong... I should've looked more closely. :o

Okay, now I'm somewhat on the right track - the numbers and operators are changing with each question, as expected. The code is now as follows:

Code :

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

public class RandomMaths {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String user;

user = scan.nextLine();
System.out.println("Hello " + user + ", let's begin.\n");
playGame();
System.out.println("Thank you for playing; hope to see you again soon!");
}

public static void playGame() {
Random rand = new Random();
int score = 0;

for(int count = 1; count <= 10; count++) {
int firstNumber = rand.nextInt(8) + 11;
int secondNumber = rand.nextInt(9) + 1;
char operator = rand.nextBoolean() ? '+' : '-';
int rightAnswer = firstNumber + operator + secondNumber;

System.out.print(count + ".   " + firstNumber + " " + operator + " " + secondNumber + " = ");
System.out.println("Correct!\n");
score++;
} else {
}
}

if (score == 10) {
System.out.println("You scored " + score + " - excellent!");
} else if (score > 5) {
System.out.println("You scored " + score + " - good job.");
} else if (score > 1) {
System.out.println("You scored " + score + " - you need to try harder.");
} else {
System.out.println("You scored " + score + " - it'd be worth getting extra help.");
}
}
}

But the output once I start to answer the questions is still not what I'm expecting (the name input is working fine):

Code :

1.   14 + 6 = 20
Incorrect - right answer is 63.

2.   16 - 5 = 11
Incorrect - right answer is 66.

3.   14 + 1 = 15
Incorrect - right answer is 58.

//...

9.   11 + 5 = 16
Incorrect - right answer is 59.

10.   14 + 8 = 22
Incorrect - right answer is 65.

You scored 0 - it'd be worth getting extra help.
Thank you for playing; hope to see you again soon!

So now the problem lies in how I'm declaring the 'rightAnswer' variable.
• April 7th, 2013, 06:57 PM
Norm
Re: Random maths operator generation
A char variable has an int value and can be added to an int. The expression:
Code :

int rightAnswer = firstNumber + operator + secondNumber;

is adding three int values together. operator has an int value. Copy and execute this:
Code :

System.out.println("+="+ (int)'+'); //+=43

The expression must be coded with the correct operator when it is compiled.

Hint: select the operator and do the math at the same time using an if else statement
• April 8th, 2013, 05:49 AM
dk87
Re: Random maths operator generation
Okay, I've finally cracked it - I tried using a switch statement again and it's working correctly. Here's the code now:

Code :

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

public class MathsQuestions {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String user;

user = scan.nextLine();
System.out.println("Hello " + user + ", let's begin.\n");
playGame();
System.out.println("Thank you for playing; hope to see you again soon!");
}

public static void playGame() {
Random rand = new Random();
int score = 0;

for(int count = 1; count <= 10; count++) {
int firstNumber = rand.nextInt(8) + 11;
int secondNumber = rand.nextInt(9) + 1;
int num = rand.nextInt(2) + 1;
char operator;

switch (num) {
case 1: operator = '+';
break;
case 2: operator = '-';
break;
}

if (num == 1) {
System.out.print(count + ".   " + firstNumber + " + " + secondNumber + " = ");
System.out.println("Correct!\n");
score++;
} else {
}
} else if (num == 2) {
System.out.print(count + ".   " + firstNumber + " - " + secondNumber + " = ");
System.out.println("Correct!\n");
score++;
} else {
}
}
}

if (score == 10) {
System.out.println("You scored " + score + " - excellent!");
} else if (score > 5) {
System.out.println("You scored " + score + " - good job.");
} else if (score > 1) {
System.out.println("You scored " + score + " - you need to try harder.");
} else {
System.out.println("You scored " + score + " - it'd be worth getting extra help.");
}
}
}

And the output when I tested it:

Code :

1.   11 - 4 = 7
Correct!

2.   15 + 5 = 20
Correct!

3.   16 - 3 = 13
Correct!

4.   11 - 5 = 6
Correct!

5.   12 - 9 = 9
Incorrect - right answer is 3.

6.   13 - 8 = 0
Incorrect - right answer is 5.

7.   15 + 6 = 21
Correct!

8.   14 - 6 = 8
Correct!

9.   16 - 4 = 12
Correct!

10.   17 - 4 = 14
Incorrect - right answer is 13.

You scored 7 - good job.
Thank you for playing; hope to see you again soon!

Thanks for pointing me in the right direction - much appreciated. :)
• April 8th, 2013, 06:11 AM
Norm
Re: Random maths operator generation
rightAnswer could be computed in the switch statement when the operator is set.

Why use a switch and an if/else if. Use one OR the other, not both.