【发布时间】:2017-02-02 14:24:00
【问题描述】:
我的钱包程序有一个简单但令人不满的问题,向我的钱包添加钱时似乎一切正常,但当我输入“-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);
}
【问题讨论】:
-
在逐行调试的过程中,您了解到...?
-
你应该把它分解成更小的方法。这是不必要的难以遵循。
-
您还应该正确缩进代码。大多数人会发现 4 个空格更适合 java 语法。
-
... 并删除重复代码。您只需要一个
wallet.add(money.GetDenom());-- 在一系列if语句之后,而不是在if语句的每个“then”块内 -
与您的问题没有直接关系,但养成从不写一个空的
catch块的习惯是个好主意。至少,把ex.printStackTrace();放在那里。如果出现问题,您想知道原因和位置,对吗?