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.

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.

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

Joined: Jun 28, 2008
Posts: 99

posted

0

Hi Gamini,

Originally posted by Gamini Sirisena: 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..

Oh? I thought, he wanted a randomly generated operator (creating a random math sign). No??

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 :-)

I'm not sure if this answer qualifies for the beginners forum but how about starting with something like this:

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

goosetap
Greenhorn

Joined: Oct 30, 2008
Posts: 5

posted

0

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!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40436

28

posted

0

Originally posted by dale truter: . . . help my little cousin learn more about math. thanks guys!

You're welcome But why didn't you ask your cousin to help?

goosetap
Greenhorn

Joined: Oct 30, 2008
Posts: 5

posted

0

Originally posted by Campbell Ritchie:

You're welcome But why didn't you ask your cousin to help?

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.

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99

posted

0

Hi Dale,

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

Joined: Oct 13, 2005
Posts: 40436

28

posted

0

There is another way to do it using a char[] array for the operators and a switch block.

goosetap
Greenhorn

Joined: Oct 30, 2008
Posts: 5

posted

0

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

Joined: Oct 30, 2008
Posts: 5

posted

0

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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40436

28

posted

0

Of course you can use "cases;" I said earlier you could use a switch block.