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

时间:2019-05-20 06:45:48

标签: java java.util.scanner

我正在尝试将货币的价值从美元转换为每种硬币的百分比(以分表示)。我想使此代码可重复,而无需重新运行脚本。我遇到的问题在下面列出;

  • 使此可重复性/改进我的代码的另一种方法是
  • 使用后,键入第二个输入,而不是输出第一个输出的代码
  • 捕获异常失效检查器在第二次尝试后将无法工作
  • 我该如何以以下格式重复编写此代码
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);

    }
}

(预期格式示例)

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

1 个答案:

答案 0 :(得分:0)

您可以将布尔值更改为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;
    }
  }

然后可以将布尔值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布尔值。

出于格式化目的,将System.out.print("Enter a dollar 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:");

    }

现在几行的输出将是:

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