# Creating a random math sign

goosetap

Greenhorn

Posts: 5

posted 7 years ago

Hi everybody,

I'm very new to java but I am eager to learn a little about it. My question is that I would like to make a simple program that will add, subtract, multiply or divide two numbers. Thr thing is I would like to make the mathematical symbol randomly generated. I have learned how to create randomly generated numbers but don't know how to make a randomly generated symbol.

I would guess that I need to use an array, but I'm not sure if I'm right or wrong about that.

The idea I'm trying to get is that I would like to create a little test where two random numbers are generated, either added, multiplied, divided or subtracted to or by one another and the answer is given. The mathematical symbol used, such as '+' will be represented as a '?' in the program and my user will have to guess what symbol was used.

Can somebody help me to create the random symbols. The reason I think I need an array is because, if I am correct, arrays hold information of similar types in groups. I also read somewhere that the symbols have numerical values, but I'm not too sure about that either.

Thanks for any help guys.

I'm very new to java but I am eager to learn a little about it. My question is that I would like to make a simple program that will add, subtract, multiply or divide two numbers. Thr thing is I would like to make the mathematical symbol randomly generated. I have learned how to create randomly generated numbers but don't know how to make a randomly generated symbol.

I would guess that I need to use an array, but I'm not sure if I'm right or wrong about that.

The idea I'm trying to get is that I would like to create a little test where two random numbers are generated, either added, multiplied, divided or subtracted to or by one another and the answer is given. The mathematical symbol used, such as '+' will be represented as a '?' in the program and my user will have to guess what symbol was used.

Can somebody help me to create the random symbols. The reason I think I need an array is because, if I am correct, arrays hold information of similar types in groups. I also read somewhere that the symbols have numerical values, but I'm not too sure about that either.

Thanks for any help guys.

Campbell Ritchie

Sheriff

Posts: 49733

69

posted 7 years ago

Welcome to JavaRanch

I think you are on the right lines.

You obviously know about arrays, and you obviously know that the char primitive type is actually stored as an unsigned 16-bit integer. And you obviously know that you can convert numbers to symbols if you know their Unicode values (try here). And you presumably know how you can set up a switch with

Now you have to find the Random class and see whether you can get a random

I think you are on the right lines.

You obviously know about arrays, and you obviously know that the char primitive type is actually stored as an unsigned 16-bit integer. And you obviously know that you can convert numbers to symbols if you know their Unicode values (try here). And you presumably know how you can set up a switch with

**int**values, but if you try a**char**instead it works just as well because a**char**can be cast to an**int**.Now you have to find the Random class and see whether you can get a random

**int**up to the size of your array out of it.
Ls chin

Ranch Hand

Posts: 99

posted 7 years ago

I don't know if this is what you need but I think it should work.

Good luck.

Originally posted by dale truter:

I have learned how to create randomly generated numbers but don't know how to make a randomly generated symbol.

I don't know if this is what you need but I think it should work.

Good luck.

Gamini Sirisena

Ranch Hand

Posts: 378

posted 7 years ago

Either I got it or totally lost it...

I reckon that dale wants to present two numbers and an answer and the user has to guess what operator was used to arrive at the answer..

First thing that came to mind was the modulus operator %

So as campbell said find the Random class generate a random int do a modulo 4

eg. 14%4 gives 2. The remainder values can be only 0,1,2 and 3. So when you get 0 do an add, when 1 do a substract etc..

I reckon that dale wants to present two numbers and an answer and the user has to guess what operator was used to arrive at the answer..

First thing that came to mind was the modulus operator %

So as campbell said find the Random class generate a random int do a modulo 4

eg. 14%4 gives 2. The remainder values can be only 0,1,2 and 3. So when you get 0 do an add, when 1 do a substract etc..

Ls chin

Ranch Hand

Posts: 99

posted 7 years ago

You are both right - he wants to generate an operator randomly - then perform that operation on two numbers. LS Chin has good code for generating the operator. Gemini correctly states that you would then use the generated value to determine what operation to do (he just generated the value differently). When you put both your solutions together, you settle on what Campbell said :-)

Steve

Garrett Rowe

Ranch Hand

Posts: 1296

goosetap

Greenhorn

Posts: 5

posted 7 years ago

thanks a lot guys, Gamini has it right! Just to clarify, I would like my user to see something along the lines of:

7 ? 3 = 21

The 7 and the 3 will be randomly generated. In this case, the 7 has been multiplied. The mathematical symbol will be randomly generated just as the 3 and the 7 were.

If the user repeats the program, a whole new random sum will be generated. I'm making the program to help my little cousin learn more about math. I haven't yet tried the codes that have been posted, but thanks very much for taking the time to do that for me.

I'll post up the code when I've tried it and if there's anything I don't understand, hopefully somebody can explain it to me. thanks guys!

7 ? 3 = 21

The 7 and the 3 will be randomly generated. In this case, the 7 has been multiplied. The mathematical symbol will be randomly generated just as the 3 and the 7 were.

If the user repeats the program, a whole new random sum will be generated. I'm making the program to help my little cousin learn more about math. I haven't yet tried the codes that have been posted, but thanks very much for taking the time to do that for me.

I'll post up the code when I've tried it and if there's anything I don't understand, hopefully somebody can explain it to me. thanks guys!

Campbell Ritchie

Sheriff

Posts: 49733

69

goosetap

Greenhorn

Posts: 5

Ls chin

Ranch Hand

Posts: 99

posted 7 years ago

Hi Dale,

This is for your cousin, I hope he/she likes maths. It is not a full-proof program and has rooms for improvements but it's a start. Hope your cousin will enjoy it.

Enjoy!

Originally posted by dale truter:

haha, I tried but he uses such big words that I couldn't make heads or tails of what he was saying. 6 year olds, I tell you.

This is for your cousin, I hope he/she likes maths. It is not a full-proof program and has rooms for improvements but it's a start. Hope your cousin will enjoy it.

Enjoy!

Campbell Ritchie

Sheriff

Posts: 49733

69

goosetap

Greenhorn

Posts: 5

posted 7 years ago

Wow! That works perfectly! Thanks from me and little Danny! The only trouble is I have no clue why it works It is waaaaaaaaaaay beyond my knowledge haha

Originally posted by LS chin:

Hi Dale,

This is for your cousin, I hope he/she likes maths. It is not a full-proof program and has rooms for improvements but it's a start. Hope your cousin will enjoy it.

Enjoy!

Wow! That works perfectly! Thanks from me and little Danny! The only trouble is I have no clue why it works It is waaaaaaaaaaay beyond my knowledge haha

goosetap

Greenhorn

Posts: 5

posted 7 years ago

Actually guys, thinking about it, could the answers be selected using case statements? For example, I give a selection:

1 = +

2 = *

3 = /

4 = -

The user would type the corresponding number to input an answer. I suppose it would require four case statements where;

if "+" is correct print "correct" else print "incorrect".

if "*" is correct print "correct" else print "incorrect".

if "/" is correct print "correct" else print "incorrect".

if "-" is correct print "correct" else print "incorrect".

Do you see what I mean? I know it's probably a long-winded way of doing it but it's more of my "level" of knowledge, I guess.

1 = +

2 = *

3 = /

4 = -

The user would type the corresponding number to input an answer. I suppose it would require four case statements where;

if "+" is correct print "correct" else print "incorrect".

if "*" is correct print "correct" else print "incorrect".

if "/" is correct print "correct" else print "incorrect".

if "-" is correct print "correct" else print "incorrect".

Do you see what I mean? I know it's probably a long-winded way of doing it but it's more of my "level" of knowledge, I guess.