只接受 Java 中的有效输入并输出余数

Only accepting valid inputs in Java and outputting remainder

我正在尝试编写接受有效输入($0.05、$0.10、$0.20、$0.50、$1.00、$2.00、$5.00、$10.00、$20.00、$50.00、$100.00)并输出任何余数的代码,输出代码类似于:

    .00 remains to be paid. Insert money: .00
    You gave .00.
    .00 remains to be paid. Insert money: 3
    Invalid value. Try again.
    .00 remains to be paid. Insert money: 
    Invalid value. Try again.
    .00 remains to be paid. Insert money: .00
    You gave .00
    .00 remains to be paid. Insert money: .00
    Invalid value. Try again.

不幸的是,目前,当我输入像“3”这样的整数时,它有时会输出 "empty string",有时会输出 "Invalid value. Try again.",但仍然将整数的值作为 paidTotal 的一部分,即

    .00 remains to be paid. Insert money: .00
    You gave .00.
    .00 remains to be paid. Insert money: 4
    empty String
    .00 remains to be paid. Insert money: .00
    You gave .00.
    .00 remains to be paid. Insert money: 9
    empty String
    .00 remains to be paid. Insert money: 9
    Invalid value. Try again.
    .00 remains to be paid. Insert money: .00
    You gave .00.
    .00 remains to be paid. Insert money: .00
    You gave .00
    .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("[=13=].05", "[=13=].10", "[=13=].20", "[=13=].50", ".00", ".00", ".00", ".00", ".00", ".00", "0.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();
            }
         }
       }
    }

您有时会看到 "Empty String" 的原因是,对于某些输入,您没有包括美元符号。所以 String noDollar = moneyEntered.substring(1); 跳过唯一的字符,然后你有空字符串。 "Invalid value. Try again." 当包含美元符号但货币值不在 ArrayList 中时正确出现。

在验证 validMoney 包含 moneyEntered 之前,您还将 moneyAsDouble 添加到 paidTotal。因此,无论输入是否有效,double 值都将添加到 paidTotal。此外,在 while 循环中,您再次将 moneyAsDouble 添加到 paidTotal

我认为这里有多个问题可以解决。逐步浏览代码,看看它做了什么。您会看到订单失败的地方。

我建议将输入视为数字。在这种情况下,您可以(大量)简化验证和输入处理。

显示 Empty String 是因为有时您在键入值时不包括美元符号,并且因为 .substring(1) returns 是一个子字符串开头的字符串对于指定索引处的字符,在您的情况下 1,它会跳过一个且仅一个字符。此外,您还有一些其他细节需要注意...我对您的代码进行了一些重构,希望对您有所帮助:)

final Set<Double> VALID_INPUT = new HashSet<>(Arrays.asList(0.05, 0.10, 0.20, 0.50, 1.00,
    2.00, 5.00, 10.00, 20.00, 50.00, 100.00));
final Scanner scanner = new Scanner(System.in);
//final double priceSum = 104.89d; // This should be a parameter or an input
double acc = 0.00d;

System.out.printf("$%.2f remains to be paid. Insert money: $", priceSum);

String input = scanner.nextLine();

while (acc < priceSum) {
  final Double amount = Double.valueOf(input); // TODO: Handle invalid format here

  // Uncomment the second one to avoid negative values (if not allowed)
  if (VALID_INPUT.contains(value)/* && (acc + amount < priceSum)*/) {
    acc += value;
    System.out.printf("You gave $%.2f%n", value);
  } else {
    System.out.println("Invalid value. Try again...");
  }
  System.out.printf("$%.2f remains to be paid. Enter coin or note: $", priceSum - acc);
  input = scanner.nextLine();
}