-1
私のウォレットプログラムでは、不満な問題がありましたが、私の財布にお金を追加するとすべてうまくいくように見えますが、私のdo-whileループ私のmoneyChoiceとして "-1"を入力すると。あなたがmoneyChoice
を読んだ後Javaを取得できない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);
}
そして、行ごとにデバッグしている間に、あなたはそれを学んだ...? –
'moneyChoice'の後ろにスペースを入れてください – xhulio
これを小さな方法に分割してください。それは不必要に従うのが難しいです。 – khelwood