I am stuck with this code of simple calculator . this add's ,subtracts,multiplies,and divides given two numbers as the user enter's the wrong option (i.e., not mention in the menu) the program should ask for reentering the option.I cant find out how to do this.I have started learning java from past 3 days only
Also having problem in the line 25,29,33,37 about the " = " mentioned
HERE IS THE CODE
You would find it easier if you used the % tags as in "formatting output" in this Java™ Tutorials section.
I would suggest you can simplify your code:
You only need one Scanner object.
Produce an operator String. You can have operator = "+"; or similar in your switch-case.
Then you only need one line which prints the result, after the end of the switch. You will get peculiar output if anybody enters 5, however!
Without changing anything like that, your problem is that you have results to be printed out which start with a number, and you want a String. To make them into Strings, try starting with the empty String "" and put that with a + at the beginning of each of your lines to print the result.
You are also short of a + sign in each of those print statements; it shows the hazards of copy-and-paste; you have copied the error into all the statements.
If you have only been doing Java for 3 days, it is hardly surprising you haven't worked out how to write a loop yet.
Joined: Sep 09, 2009
Thank you sir for your valuable suggestions. Your tips made me run this program smoothly and off course, i would definitely be working hard to learn all the concepts of java including loops as you find it surprising for a 3 day old student of not knowing about loops.
That means there is something wrong with my studying approach .Thank you again for helping.
Joined: Oct 13, 2005
What I actually said was that I did not expect anybody to be familiar with loops after 3 days.
Of course, you have a whole week's experience now . . .