Little Java Help :)

ChrisCramer247

Platinum Member
Dec 6, 2001
2,171
1
0
Hey Everyone Thanks For Looking!

Here's my problem. I have to create a program that does the following:

Asks for the first number
Asks for the second number
Displays the menu
Gets user choice
Performs action based on the user choice

My Prof wants this in a very strange way as well and this is where im screwing up. I was going to generate a whole new source code but she wants me to previous programs i've written. I've created the three now stuck on building the program itself. For creating a loop that causes an exit she gave us this code and I have no idea how to complete it:

String userChoice=??;
while (the userChoice is not equal to ?7?){
ask the user for his choice
read choice from keyboard (here you might want to use in.getString());
if the userChoice equals 1, then perform the necessary action (in my case, my 1 corresponds to add so I used the following:
if(userChoice.equals(?1?)) math.add(first,second); //where first and second are the variable names for my first and second numbers entered by the user.

If interested here is the link if you guys could help me fill in the missing i'd be much appreciative.



Linky Poo
 

DAGTA

Diamond Member
Oct 9, 1999
8,172
1
0
Reading in the user's choice as a string is a bad idea. Read it in as an int. If you must read it in as a string, then parse that string and save the value as an int. Then, inside of your while loop, use a switch...case structure or a nested if...else structure.
Hope that helps.
-DAGTA
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
i havent written in Java (forgive the syntax) for a while but it will look something like this:


String choice = "0";
int num = 0;

tryagain:

try{
while(Integer.Parse(choice) != 7)
{

Console.Write("Please enter your choice : ");
Console.Readline(choice);
num = Integer.Parse(choice);

switch(num)

}

}
catch(MalformedNumberException mne)
{
Console.Writeline("That was not a number please try again.");
goto tryagain;
}






__________________________________


that should get you started in the correct direction.