有効な入力($ 0.05、$ 0.10、$ 0.20、$ 0.50、$ 1.00、$ 2.00、$ 5.00、$ 10.00、$ 20.00、$ 50.00、$ 100.00) 、のような出力コードを:有効な入力をJavaで受け入れ、余りを出力する
$38.00 remains to be paid. Insert money: $20.00
You gave $20.00.
$18.00 remains to be paid. Insert money: 3
Invalid value. Try again.
$18.00 remains to be paid. Insert money: $9
Invalid value. Try again.
$18.00 remains to be paid. Insert money: $10.00
You gave $30.00
$8.00 remains to be paid. Insert money: $8.00
Invalid value. Try again.
私は「3」のように整数を入力すると、残念ながら現時点では、それは時に「空の文字列」を出力し、時には出力「無効な値を再度実行してください。」支払済合計の一部として整数の値をとります。すなわち、
$18.00 remains to be paid. Insert money: $10.00
You gave $10.00.
$8.00 remains to be paid. Insert money: 4
empty String
$8.00 remains to be paid. Insert money: $2.00
You gave $10.00.
$8.00 remains to be paid. Insert money: 9
empty String
$8.00 remains to be paid. Insert money: 9
Invalid value. Try again.
$8.00 remains to be paid. Insert money: $5.00
You gave $10.00.
$8.00 remains to be paid. Insert money: $2.00
You gave $12.00
$6.00 remains to be paid. Insert money:
ここに私のコードの関連するセクションがあります。どんな助けでも大歓迎です!
System.out.print("$" + formatter.format(priceSum) + " remains to be paid. Insert money: ");
String moneyEntered = keyboard.nextLine();
System.out.println("");
String noDollar = moneyEntered.substring(1);
double moneyAsDouble = Double.parseDouble(noDollar);
double paidTotal = 0;
paidTotal += moneyAsDouble;
List validMoney = Arrays.asList("$0.05", "$0.10", "$0.20", "$0.50", "$1.00", "$2.00", "$5.00", "$10.00", "$20.00", "$50.00", "$100.00");
boolean moneyEnteredFound = true;
while(true) {
if (validMoney.contains(moneyEntered)) {
while (paidTotal < priceSum) {
if (validMoney.contains(moneyEntered)) {
System.out.println("You gave $" + formatter.format(paidTotal));
System.out.print("$" + formatter.format(priceSum - paidTotal) + " remains to be paid. Enter coin or note: ");
moneyEntered = keyboard.nextLine();
noDollar = moneyEntered.substring(1);
moneyAsDouble = Double.parseDouble(noDollar);
paidTotal += moneyAsDouble;
} else {
System.out.println("Invalid value. Try again.");
System.out.print("$" + formatter.format(priceSum - paidTotal) + " remains to be paid. Enter coin or note: ");
moneyEntered = keyboard.nextLine();
}
}
}
}
フィードバックありがとうございました! 'double paidTotal = 0'を残して' moneyAsDouble'を追加しないと、 '$ 0.00 'を返します。あなたが私のループの構造についていくつかの指針を与えることができれば、それはすばらしいことでしょう。 – sa4an