Plan 9
May 27th, 2002, 08:07 PM
I need to print five options, collect a keystroke, and return the right amount if the user intered 1 through 5.
So far it works, But if they press anything else it needs to reprint the menu and select again.
Since there is no goto I was tring to loop while not one of the correct choices.
Heres the weird part. If you select the wrong answer, it will print the menu three times, then wait for your response.
I've been screwing with this all day, and it allways prints three times after a wrong answer.
If anyone can help sort through this, it would be great. This is how we learn. :)
public class Investment3
{
public static void main(String arguments[]) throws Exception
{
Investment3 in = new Investment3();
char amountC, yearC;
int amountI, yearI;
amountI = in.menuAmount();
System.out.println("amount = " + amountI);
}
private int menuAmount() throws Exception
{
int invAmount = 0, ctr = 0;
KeyboardIO kb1 = new KeyboardIO();
while (ctr < 2)
{
char amnt = '0';
System.out.println(" 1 - $1,000");
System.out.println(" 2 - $2,000");
System.out.println(" 3 - $3,000");
System.out.println(" 4 - $4,000");
System.out.println(" 5 - $5,000");
amnt = kb1.readChar();
switch(amnt)
{
case '1':
invAmount = 1000;
ctr = 2;
break;
case '2':
invAmount = 2000;
ctr = 2;
break;
case '3':
invAmount = 3000;
ctr = 2;
break;
case '4':
invAmount = 4000;
ctr = 2;
break;
case '5':
invAmount = 5000;
ctr = 2;
break;
default:
invAmount = 0;
ctr = 1;
}
}
return invAmount;
}
}
So far it works, But if they press anything else it needs to reprint the menu and select again.
Since there is no goto I was tring to loop while not one of the correct choices.
Heres the weird part. If you select the wrong answer, it will print the menu three times, then wait for your response.
I've been screwing with this all day, and it allways prints three times after a wrong answer.
If anyone can help sort through this, it would be great. This is how we learn. :)
public class Investment3
{
public static void main(String arguments[]) throws Exception
{
Investment3 in = new Investment3();
char amountC, yearC;
int amountI, yearI;
amountI = in.menuAmount();
System.out.println("amount = " + amountI);
}
private int menuAmount() throws Exception
{
int invAmount = 0, ctr = 0;
KeyboardIO kb1 = new KeyboardIO();
while (ctr < 2)
{
char amnt = '0';
System.out.println(" 1 - $1,000");
System.out.println(" 2 - $2,000");
System.out.println(" 3 - $3,000");
System.out.println(" 4 - $4,000");
System.out.println(" 5 - $5,000");
amnt = kb1.readChar();
switch(amnt)
{
case '1':
invAmount = 1000;
ctr = 2;
break;
case '2':
invAmount = 2000;
ctr = 2;
break;
case '3':
invAmount = 3000;
ctr = 2;
break;
case '4':
invAmount = 4000;
ctr = 2;
break;
case '5':
invAmount = 5000;
ctr = 2;
break;
default:
invAmount = 0;
ctr = 1;
}
}
return invAmount;
}
}