[英]Can't get Java do while loop to break
A simple yet disgruntling problem with my wallet program, everything seems to be working properly when adding money to my wallet but I can't seem to break out of my do-while loop when inputing "-1" as my moneyChoice. 我的钱包程序存在一个简单但令人不解的问题,向我的钱包添加资金时,一切似乎都正常运行,但是当我输入“ -1”作为moneyChoice时,我似乎无法脱离do-while循环。
private static void AddItemToWallet()throws IOException{
boolean valid=true;
String input;
int moneyChoice;
char addToWallet;
do{
out.println("\n\n======================================================");
do{
out.println("Please enter choice to add (integer) --->");
//display money choices
System.out.println("Avaliable moneys to add:\n");
for (Money money : Money.values()) {
System.out.print(money.ordinal()+1 + " --- ");
System.out.print(money + ": ");
System.out.println(money.GetDenom());
}
//scanner=new Scanner(in);
moneyChoice=scanner.nextInt();
if (moneyChoice > 11 || moneyChoice <= -2 || moneyChoice == 0) {
out.println("Your choice is not valid.");
}
}while(moneyChoice > 11 || moneyChoice <= -2 || moneyChoice ==0);
out.println(moneyChoice);
do{
try{
valid=false;
scanner=new Scanner(in);
//confirm choice
out.println("Add item to wallet? (y/n)");
input=scanner.nextLine();
addToWallet=input.charAt(0);
if (Character.toLowerCase(addToWallet)=='n'){
out.println("Selection " + moneyChoice + " was not added to wallet.");
valid=true;
}else if (Character.toLowerCase(addToWallet)=='y'){
out.println("Selection " + moneyChoice + " was added to wallet.");
valid=true;
Money money = Money.PENNY;
if(moneyChoice ==1){
money = Money.PENNY;
wallet.add(money.GetDenom());
}
if(moneyChoice ==2){
money = Money.NICKLE;
wallet.add(money.GetDenom());
}
if(moneyChoice ==3){
money = Money.DIME;
wallet.add(money.GetDenom());
}
if(moneyChoice ==4){
money = Money.QUARTER;
wallet.add(money.GetDenom());
}
if(moneyChoice ==5){
money = Money.HALFDOLLAR;
wallet.add(money.GetDenom());
}
if(moneyChoice ==6){
money = Money.ONE;
wallet.add(money.GetDenom());
}
if(moneyChoice ==7){
money = Money.FIVE;
wallet.add(money.GetDenom());
}
if(moneyChoice ==8){
money = Money.TEN;
wallet.add(money.GetDenom());
}
if(moneyChoice ==9){
money = Money.TWENTY;
wallet.add(money.GetDenom());
}
if(moneyChoice ==10){
money = Money.FIFTY;
wallet.add(money.GetDenom());
}
if(moneyChoice ==11){
money = Money.HUNDRED;
wallet.add(money.GetDenom());
}
System.out.println(money);
//Using a Switch and set ENUM
Money foundMoney = money;
switch (foundMoney) {
case PENNY:
money = Money.PENNY;
System.out.println("Pennies are useless");
break;
case NICKLE:
System.out.println("Why did the fan go to the concert? To get their Nickle back!!! Man are they horrible.");
break;
case DIME:
System.out.println("How come Dimes are the smallest coin? So strange.");
break;
case QUARTER:
System.out.println("Tom Brady, best Quarterback eva!!!!");
break;
case HALFDOLLAR:
System.out.println("Half Dollar coin. Have you seen one???");
break;
case ONE:
System.out.println("One is the lonliest number.");
break;
case FIVE:
System.out.println("Five Buck Lunch, here I come!!!");
break;
case TEN:
System.out.println("Ten is what grandma gave you for graduation.");
break;
case TWENTY:
System.out.println("Twenty Bucks. 2 0... awesome.");
break;
case FIFTY:
System.out.println("Fitty Sploondocks.");
break;
case HUNDRED:
System.out.println("I did it for the Benjamins. Get rich or die trying.");
break;
}
}
}catch(Exception ex){
}
if (!valid){
out.println("That is not a valid answer");
}
}while(!valid);
}while(moneyChoice!= -1);
} }
Your program can basically be reduced to the following 您的程序基本上可以简化为以下内容
do {
// read moneyChoice
do {
// Perform logic
} while ( ! valid );
} while ( moneyChoice != -1 );
Note that after you read moneyChoice
, you always go into the do
that performs the logic. 请注意,您看后
moneyChoice
,你总是进入do
执行的逻辑。 There is nothing there that checks whether the moneyChoice
you entered was -1
. 没有什么可以检查您输入的
moneyChoice
是否为-1
。 So the logic is performed anyway, and only then, when you hit while ( moneyChoice != -1 )
, it will stop. 因此无论如何都会执行逻辑,只有在您按下
while ( moneyChoice != -1 )
,逻辑才会停止。
You should add an if
such that the logic will only be performed if the moneyChoice
that was read is not -1. 您应该添加一个
if
,以便仅在读取的moneyChoice
不为-1时才执行逻辑。
do {
// read moneyChoice
if ( moneyChoice != -1 ) {
do {
// Perform logic
} while ( ! valid );
}
} while ( moneyChoice != -1 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.