gpt4 book ai didi

java - 在用户输入正确的格式后,如何使扫描仪可重复多次

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:36 25 4
gpt4 key购买 nike

我正在尝试将货币的值(value)从美元转换为每种硬币的值(value)(以美分为单位)。我想让这段代码可重复,而不需要重新运行脚本。下面列出了我遇到的问题;

  • 使此可重复性/改进我的代码的其他方法是什么
  • 使用后键入第二个输入,而不是输出第一个输出的代码
  • 第二次尝试后,捕获异常无效检查器将不起作用
  • 如何使此代码可按以下格式重复
public class MakeChange {

public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter a dollar amount:");
boolean isValidAmount = false;
String amountInString = input.nextLine();
double amount = 0;
try {
if (amountInString.isEmpty() || amountInString.equals(" ")) {
System.out.println("INVALID");
}
} catch (InputMismatchException e) {
e.printStackTrace();
}

if (amountInString.chars().allMatch(Character::isAlphabetic)) {
System.out.println("INVALID");
} else {
amount = Double.parseDouble(amountInString);
isValidAmount = true;
convert(amount);
}
while (isValidAmount = true) {
System.out.print("Enter a dollar amount:");
amountInString = input.nextLine();
convert(amount);
}
}

private static void convert(double amount) {
int toonies;
int loonies;
int quarters;
int dimes;
int nickels;
int remainingCents;

remainingCents = (int) (amount * 100 + .5);
toonies = remainingCents / 200;
remainingCents %= 200;
loonies = remainingCents / 100;
remainingCents %= 100;
quarters = remainingCents / 25;
remainingCents %= 25;
dimes = remainingCents / 10;
remainingCents %= 10;
nickels = remainingCents / 5;
remainingCents %= 5;

System.out.println("toonies:" + toonies + ";" + " loonies:" + loonies + ";" + " quarters:" + quarters + ";"
+ " dimes:" + dimes + ";" + " nickels:" + nickels);

}
}

(预期格式示例)

输入美元金额:12
图尼:6;加元:0;宿舍:0;一毛钱:0;镍币:0
输入美元金额:42
图尼:10;加元:0;宿舍:2;一毛钱:0;镍币:0
(当前问题)
输入美元金额:12
图尼:6;加元:0;宿舍:0;一毛钱:0;镍币:0
输入美元金额:42'
图尼:6;加元:0;宿舍:0;一毛钱:0;镍币:0

最佳答案

您可以将 boolean 值更改为 isValidAmount 方法:

private static boolean isValidAmount(String input) {

if (input.isEmpty() || input.equals(" ")) {
System.out.println("INVALID input:" + input);
return false;
}

if (input.chars().allMatch(Character::isAlphabetic)) {
System.out.println("INVALID input: " + input );
return false;
} else {
return true;
}
}

然后您可以将 boolean 值 isValid 移至 while 循环:

public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter a dollar amount:");
String amountInString;

while ((amountInString = input.nextLine()) != null && isValidAmount(amountInString)) {
// remove the System.out.print("Enter a dollar amount:"); from the while loop
double amount = Double.parseDouble(amountInString);
convert(amount);
}
}

这样每次迭代都会刷新 isValidAmount boolean 值。

出于格式化目的,将 System.out.print("Enter adollar amount:"); 放在 convert 方法中,而不是 while 循环中。

private static void convert(double amount) {
int toonies;
int loonies;
int quarters;
int dimes;
int nickels;
int remainingCents;

remainingCents = (int) (amount * 100 + .5);
toonies = remainingCents / 200;
remainingCents %= 200;
loonies = remainingCents / 100;
remainingCents %= 100;
quarters = remainingCents / 25;
remainingCents %= 25;
dimes = remainingCents / 10;
remainingCents %= 10;
nickels = remainingCents / 5;
remainingCents %= 5;

System.out.println("toonies:" + toonies + ";" + " loonies:" + loonies + ";" + " quarters:" + quarters + ";"
+ " dimes:" + dimes + ";" + " nickels:" + nickels + ";" + " remainingCents:" + remainingCents);
System.out.print("Enter a dollar amount:");

}

几行的输出现在将是:


输入美元金额:26.50
图尼:13;加元:0;宿舍:2;一毛钱:0;镍币:0;剩余分:0
输入美元金额:2315.90
图尼:1157;加元:1;宿舍:3;一毛钱:1;镍币:1;剩余分:0
输入美元金额:1222
图尼:611;加元:0;宿舍:0;一毛钱:0;镍币:0;剩余分:0
输入美元金额:0.05
卡通:0;加元:0;宿舍:0;一毛钱:0;镍币:1;剩余分:0
输入美元金额:0.01
卡通:0;加元:0;宿舍:0;一毛钱:0;镍币:0;剩余美分:1

关于java - 在用户输入正确的格式后,如何使扫描仪可重复多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56215574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com