[英]How do i make the scanner repeatable several time after the user input the correct format
I am trying convert the value of money from dollars to how much of each type of coin, in cent. 我正在尝试将货币的价值从美元转换为每种类型的硬币的多少,以分表示。 I want to make this code repeatable without needing to rerun the script.
我想使此代码可重复,而无需重新运行脚本。 The problem i came across is listed below;
我遇到的问题列在下面;
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);
}
}
(Example expected format) (示例预期格式)
Enter a dollar amount:12 toonies:6; loonies:0; quarters:0; dimes:0; nickels:0 Enter a dollar amount:42 toonies:10; loonies:0; quarters:2; dimes:0; nickels:0 (Current problem) Enter a dollar amount:12 toonies:6; loonies:0; quarters:0; dimes:0; nickels:0 Enter a dollar amount:42' toonies:6; loonies:0; quarters:0; dimes:0; nickels:0
You can change the boolean into a method isValidAmount: 您可以将布尔值更改为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;
}
}
You can then move the boolean isValid to your while loop: 然后,您可以将布尔值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);
}
}
this way the isValidAmount boolean is refreshed each iteration. 这样,每次迭代都会刷新isValidAmount布尔值。
For formatting purposes place the System.out.print("Enter a dollar amount:");
为了格式化,请放置
System.out.print("Enter a dollar amount:");
in the convert
method instead of the while loop. 在
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:");
}
The output for a few lines will now be: 现在,几行的输出将是:
Enter a dollar amount:26.50 toonies:13; loonies:0; quarters:2; dimes:0; nickels:0; remainingCents:0 Enter a dollar amount:2315.90 toonies:1157; loonies:1; quarters:3; dimes:1; nickels:1; remainingCents:0 Enter a dollar amount:1222 toonies:611; loonies:0; quarters:0; dimes:0; nickels:0; remainingCents:0 Enter a dollar amount:0.05 toonies:0; loonies:0; quarters:0; dimes:0; nickels:1; remainingCents:0 Enter a dollar amount:0.01 toonies:0; loonies:0; quarters:0; dimes:0; nickels:0; remainingCents:1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.