1. ## Math game

Hello, I am wondering what would be the best way to go about doing a simple math game. I want to ask the user 10 numeracy questions for example,

a * b = (user-input) - correct/wrong!
y - z = (user-input) - correct/wrong!
e + t = (user-input) - correct/wrong!

and so forth.

What I have been considering, is creating an array of integers which elements hold 20 different random numbers created by the Random util. I'm just not sure how to generate random operations, for example, I don't want to have say

Java Code:
` System.out.println(num[rand1] + " * " + num[rand2] + " = " + input);`
because then the first question will always be multiplication. how would I be able to generate random operators? Do I need to create a char array holding the math operators? and would I be able to concatenate a char array with an int array?

Thanks very much for any feedback.  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## You can use an array of char and a random index. What you concatenate are array elements not arrays. Also investigate the String and PrintStream printf() methods.  Reply With Quote

3. ## For example,

Java Code:
```import java.util.Random;
import java.util.Scanner;

public static void sums() {

char operators = new char;

operators = '+';
operators = '-';
operators = '*';
operators = '/';

Random rand1 = new Random();
int index1 = rand1.nextInt(4);

}```
Does this look about right at the moment?  Reply With Quote

4. ##  Originally Posted by Nanomech For example,

Java Code:
```import java.util.Random;
import java.util.Scanner;

public static void sums() {

char operators = new char;

operators = '+';
operators = '-';
operators = '*';
operators = '/';

Random rand1 = new Random();
int index1 = rand1.nextInt(4);

}```
Does this look about right at the moment?
Nope, that code will throw an ArrayIndexOutOfBounds exception (hint: array indexes start at zero).

kind regards,

Jos  Reply With Quote

5. ## Ah yes, I compiled and ran it and it gave me that exception. How would I display the value of whatever element the random function generates? More importantly, something I just cannot get my head around, once 2 random numbers are generated and an operator, how would I check to see that the answer is correct? Because they will be random everytime, is there a way to determine the result of the sum?

Thanks. I'm still looking at the PrintStream printf method.

Regards,

NM.  Reply With Quote

6. ##  Originally Posted by Nanomech Ah yes, I compiled and ran it and it gave me that exception. How would I display the value of whatever element the random function generates? More importantly, something I just cannot get my head around, once 2 random numbers are generated and an operator, how would I check to see that the answer is correct? Because they will be random everytime, is there a way to determine the result of the sum?

Thanks. I'm still looking at the PrintStream printf method.
If you have two operands 'left' and 'right' and an operator (char) you can calculate the result with a simple switch-statement:

Java Code:
```int left, right; // the two operands
char op; // the operator
int result;

swich (op) {
case '+': result= left+right; break;
case '-': result= left-right; break;
// etc. etc.```
kind regards,

Jos  Reply With Quote

7. ## That is awesome, thank you very much for this code.

Could I also ask do I need to create 20 Random object? Could this be done with a for loop?

Also, I was advised to use the printf method. I've looked at it but I cannot figure out the code to insert into my class. I've imported java.io.*, i've seen the signature line you put in but what goes in the parenthesis?

Regards,

NM.   Reply With Quote

8. ##  Originally Posted by Nanomech That is awesome, thank you very much for this code.

Could I also ask do I need to create 20 Random object? Could this be done with a for loop?

Also, I was advised to use the printf method. I've looked at it but I cannot figure out the code to insert into my class. I've imported java.io.*, i've seen the signature line you put in but what goes in the parenthesis?
Yep, if you have to generate 20 random numbers you can easily use a for loop for the purpose. For that printf( ... ) method read the API documentation; it tells you all about its use. May I suggest you create a simple class Question that generates one equation with a pseudo random operator and a few handy methods, such as toString(), getAnswer() etc.

kind regards,

Jos  Reply With Quote

9. ## So could it possibly look like this?

Java Code:
```public void equation(char operator) {

answer = (left + operator + right);

}```  Reply With Quote

10. ##  Originally Posted by Nanomech So could it possibly look like this?

Java Code:
```public void equation(char operator) {

answer = (left + operator + right);

}```
Have you tried a simple example? If you had, you could've noticed that this doesn't work at all. You are just guessing now ...

kind regards,

Jos  Reply With Quote

11. ## I was guessing yes because I am not quite sure how to create what you are telling me to. I've tried to read the official documentation on several occasions now, but just do not understand what it's saying to me.

This is probably a bit out of my league tbh.

Back to basics I go^^  Reply With Quote

12. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## I wouldn't worry too much about an object oriented solution (Question class etc) if that's new to you - although it is the way to go. You can do what you want with an array of char from which you select a random index. Getting the "answer" involves a switch on the index as described above.  Reply With Quote

13. ## Java Code:
```	 		 //char array which elements hold the mathematical operators
char[] operators = new char;

operators = '+';
operators = '-';
operators = '/';
operators = '*';
//END OF CHAR ARRAY

int[] chosenone = new int;

for (int x = 1;x<=20;x++) {

Random rand = new Random();
chosenone[x] = rand.nextInt(30);
System.out.println(chosenone[x]);
}```
Ok, so I have a char array holding the mathematical operators and also an integer array holding random numbers generated with the Random util and a for loop.

Should my next step be to create the PrintStream printf method?  Reply With Quote

14. ## So is it the switchstatement that does all the equations? Would the switch statement have to fall inside the for loop?  Reply With Quote

15. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## At the start of the thread you said you wanted to ask the user 10 simple questions. The way I would approach this would be to write a program that just asked a single question: in other words there would be no for loop.

The program would choose a first random number, a second random number and a random char representing the operation. It would put these together and display the question and get the user's answer. It could also calculate the correct answer (using the switch statement) and give some feedback or whatever.

Once this simple version was correct and working it would be time to think about the for loop and how the whole pose question, get answer, check answer cycle could be repeated 10 times. But first things first. I would start by posing a single question and checking the user's response.  Reply With Quote

16. ## That's actually a really logical way to think about it and makes much sense!

I am capable of doing the following:

So when you say the program would choose 2 random numbers and 1 char, I'm guessing (just for now), that I would create the 2 random numbers using 2 different Random objects. If this is correct, would I need to create another Random object to get the index of whichever operator in a char array? Do i even need to create a char array? I know how to get data off a user and display the questions etc but it's the whole idea of which way to go about it.

I'm really sorry but I want to understand everything I need to do. I do appreciate the feedback though.  Reply With Quote

17. ##  Originally Posted by Nanomech So when you say the program would choose 2 random numbers and 1 char, I'm guessing (just for now), that I would create the 2 random numbers using 2 different Random objects. If this is correct, would I need to create another Random object to get the index of whichever operator in a char array? Do i even need to create a char array? I know how to get data off a user and display the questions etc but it's the whole idea of which way to go about it.

I'm really sorry but I want to understand everything I need to do. I do appreciate the feedback though.
A Random object is a (pseudo) random number generator; it is very well capable of generating more than one number:

Java Code:
```String operators= "+-*/";
Random r= new Random();
int left= r.nextInt(100); // generate a number in the range [0, 100)
int right= r.nextInt(100); // ditto
char op= operators.charAt(r.nextInt(operators.length()));```
I didn't comment the last line; you have to figure out how it works.

kind regards,

Jos  Reply With Quote

18. ## Java Code:
`char op= operators.charAt(r.nextInt(operators.length()));`
Ok i'll have a go at this.

Ok so you're searching the String operators using the charAt method. The charAt method searches the r.nextInt for a character position. Because r.nextInt returns a value between 0-100 it is set to the length of the String operators using operators.length() which is 3.

It then stores a number in op between 0-3. Because the charAt method was used, it returns the value not the index position.. Hope I am nearly there...

edit: oh no, does it create another Random object which is set to the length of the String operators?
---------------------------------------------------------------------------------------------------------------------

This is wrong I think, it searches the operators variable using the charAt method. It creates a new Random object and sets it's length between 0 and 3. This will return whatever value the random object chose. It then stores the value in op.
Last edited by Nanomech; 08-27-2011 at 01:58 PM.  Reply With Quote

19. ##  Originally Posted by Nanomech Java Code:
`char op= operators.charAt(r.nextInt(operators.length()));`
Ok i'll have a go at this.

Ok so you're searching the String operators using the charAt method. The charAt method searches the r.nextInt for a character position. Because r.nextInt returns a value between 0-100 it is set to the length of the String operators using operators.length() which is 3.

It then stores a number in op between 0-3. Because the charAt method was used, it returns the value not the index position.. Hope I am nearly there...
Almost right; work from the inside out: the operators.length() method returns 4; so r.nextInt(4) returns a random number 0, 1, 2 or 3; the character at that position from the String operators is assigned to variable op. If the entire thing is evaluated, variable op contains one of the characters from the String operators.

kind regards,

Jos  Reply With Quote

20. ## I did edit my post and put this:

---------------------------------------------------------------------------------------------------------------------

This is wrong I think, it searches the operators variable using the charAt method. It creates a new Random object and sets it's length between 0 and 3. This will return whatever value the random object chose. It then stores the value in op.

Which i believe to be, in other words, what you said?

Awesome that's great, so is it now a case of displaying the question and getting the user input?

Thanks so much.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•