根据测试用例获取输入的最佳方法是什么?

时间:2019-07-05 14:28:46

标签: java

我想按照这种方式进行输入。输入以测试用例的数量x开头,在接下来的x行中,有两个数字m和n。 例如:-

2
1 10
3 5

但是我想在输入后调用一个函数。我想将输入值传递给函数以打印我的输出。我尝试过这种方式,但是它首先调用了该函数。 基本上我想先获取所有输入,然后为每个测试用例调用函数以获取输出。 下面是我尝试的代码。

public static void main(String[] args) {

      Scanner in = new Scanner(System.in);
      int x = in.nextInt();
      for(int i=0 ; i<x ; i++) {
          int n = in.nextInt();
          int m = in.nextInt();
          PrimeFactors(n , m);
          System.out.println();
      }
}

2 个答案:

答案 0 :(得分:1)

您应该在循环后调用函数。

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int x = in.nextInt();
    int[][] array = new int[x][2];
    for(int i=0 ; i<x ; i++) {
        array[i][0] = in.nextInt();
        array[i][1] = in.nextInt();
    }

    for (int[] ints : array) {
        PrimeFactors(ints[0], ints[1]);
    }
}

答案 1 :(得分:0)

我建议您将输入和算法分开。

您只需将PrimeFactors(n , m);提取到一个单独的类中,然后通过常规单元测试进行测试。