java - 检测空输入(JAVA)

标签 java java.util.scanner

所以我的程序必须计算一个人在扫描仪方法中输入的零钱数量,但是,如果 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/

相关文章:

java - 为什么运行 Camel 示例需要在主线程上 hibernate

java - JTextField getText() 不起作用

java - 在企业 Java 应用程序中,SQL 位于何处?

java - 使用扫描仪时不输出数组值

java - 如何从文件中读取二维数组索引(IE : Coords) in java

java - Android通知进度条卡住

java - 有没有办法在调用 setSelectedItem() 时阻止 Action 监听器触发?

java - 如何扫描循环内的行并将其插入到ArrayList中?

java Scanner.hasNext() 用法

java - 如何使用 Scanner 将多种数据类型文件读入 ArrayList