所以我的程序必须计算一个人在扫描仪方法中输入的零钱数量,但是,如果 a) 输入不是数字和 b) 如果缺少输入,则必须弹出两个错误。我对后者有很多麻烦。
import java.util.InputMismatchException;
import java.util.Scanner;
public class MakingChange
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); // Reading from System.in
System.out.print("How much money do you have: ");
double amountInDollars = 0;
try {
amountInDollars = input.nextDouble();
} catch (InputMismatchException e) {
System.out.println("INVALID"); //print invalid
return;
}
if (input.equals(" ")){
System.out.println("INVALID");
return;
}
int amountInPennies = (int) Math.round(amountInDollars * 100);
amountInPennies = (int) (Math.round(amountInPennies / 5.0) * 5);
//toonies
int numberofToonies = (int)(amountInPennies / 200.00);
amountInPennies = amountInPennies % 200;
//loonies
int numberofLoonies = (int) (amountInPennies / 100.00);
amountInPennies = amountInPennies % 100;
//quarters
int numberofQuarters = (int)(amountInPennies / 25.00);
amountInPennies = amountInPennies % 25;
//dimes
int numberofDimes = (int)(amountInPennies / 10.00);
amountInPennies = amountInPennies % 10;
//nickels
int numberofNickels = (int)(amountInPennies / 5.00);
System.out.println("toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels);
}
}
所以预期应该是“INVALID”,但目前,返回的只是一个空格。 谢谢!
最佳答案
检查下面的代码,我添加了注释。
import java.util.Scanner;
public class MakingChange {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Reading from System.in
System.out.print("How much money do you have: ");
double amountInDollars = 0;
String amountInString = input.nextLine();
boolean isValidNum = false;
if (amountInString.equals("") || amountInString.equals(" ")) { // Empty string check
System.out.println("Empty String");
} else if (amountInString.matches("-?\\d+(\\.\\d+)?")) { // valid double check
amountInDollars = Double.parseDouble(amountInString);
isValidNum = true;
} else {
System.out.println("Number Format error");
}
if (isValidNum) {
int amountInPennies = (int) Math.round(amountInDollars * 100);
amountInPennies = (int) (Math.round(amountInPennies / 5.0) * 5);
// toonies
int numberofToonies = (int) (amountInPennies / 200.00);
amountInPennies = amountInPennies % 200;
// loonies
int numberofLoonies = (int) (amountInPennies / 100.00);
amountInPennies = amountInPennies % 100;
// quarters
int numberofQuarters = (int) (amountInPennies / 25.00);
amountInPennies = amountInPennies % 25;
// dimes
int numberofDimes = (int) (amountInPennies / 10.00);
amountInPennies = amountInPennies % 10;
// nickels
int numberofNickels = (int) (amountInPennies / 5.00);
System.out.println("toonies:" + numberofToonies + ";" + " loonies:"
+ numberofLoonies + ";" + " quarters:" + numberofQuarters
+ ";" + " dimes:" + numberofDimes + ";" + " nickels:"
+ numberofNickels);
}
}
}
关于java - 检测空输入(JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37800423/