如何在 java 中处理不同类型的输入

How to handle different types of input in java

本题

Write a program which demonstrates method overloading having the following method:

1.Interest_rate*(int s, int a, float d)
2.Interest_rate*(float s, int a, float d)
3.Interest_rate*(int s, int a, double d)

and these are the test cases which are based on the input format of the method:

Test Case 1

1000 66 12.1f

Test Case 2

1000f 45 12.1f

那么我如何创建主函数,其中输入将自动触发所需的函数,就像在 测试用例 1 中一样,它应该调用 1.Interest_rate测试用例2应该调用2.Interest_rate等等

我们只考虑前两个重载。您必须检查提供给程序的第一个数字是浮点数还是整数。根据您的输入定义,您可以这样做:

if ( args[0].endsWith("f") )
  // First number is a float
  Interest_rate(Float.parseFloat(args[0]), Integer.parseInt(args[1]), Float.parseFloat(args[2]));
else
  // First number is an integer
  Interest_rate(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Float.parseFloat(args[2]));

如您所见,在这两种情况下我们都调用了一个方法 Interest_rate 但类型不同。编译器将 select 正确重载。

我建议使用正则表达式匹配器对输入案例进行排序。

an integer can be detected as -?\d+

a float like in your examples as -?\d+(\.\d+)?f

a double as -?\d+\.\d+

whitspace as \s one or more whitespaces \s+

可能对 Java 正则表达式的研究通常会有帮助。

接下来你要明白什么是方法重载

经过一番研究,您会发现基本上是您可以创建多个具有相同名称但输入参数类型不同的方法。

如果你已经完全理解了上面的主题,(加上扫描仪的工作原理)大体上,你就可以开始实施了。

对于你做了所有这些但仍然卡住的情况,下面的问题实施可能会对你有所帮助。但一如既往:只使用你完全理解的代码,否则他会反击。

import java.util.Scanner;

public class ScannerWithDifferentInputs {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String input = sc.nextLine();
            if (input.matches("-?\d+\s+\d+\s+\d+(\.\d+)?f")) {
                input.replace("f", "");
                String[] parts = input.split("\s+");
                interestRate(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Float.parseFloat(parts[2]));
            } else if (input.matches("-?\d+(\.\d+)?f\s+\d+\s+\d+(\.\d+)?f")) {
                input.replace("f", "");
                String[] parts = input.split("\s+");
                interestRate(Float.parseFloat(parts[0]), Integer.parseInt(parts[1]), Float.parseFloat(parts[2]));
            } else if (input.matches("-?\d+\s+\d+\s+\d+\.\d+")) {
                String[] parts = input.split("\s+");
                interestRate(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Double.parseDouble(parts[2]));
            } else if (input.equals("q")) {
                System.out.println("exit");
                break;
            } else {
                System.out.println("Invalid Input");
            }
        }
    }

    static void interestRate(int i1, int i2, float f) {
        System.out.println("case int, int, float");
        System.out.println(i1 + " " + i2 + " " + f);
    }

    static void interestRate(float f1, int i, float f2) {
        System.out.println("case float, int, float");
        System.out.println(f1 + " " + i + " " + f2);
    }

    static void interestRate(int i1, int i2, double d) {
        System.out.println("case int, int, double");
        System.out.println(i1 + " " + i2 + " " + d);
    }

}