多个输入并立即获得输出

时间:2015-07-15 09:16:32

标签: java

我打算在给定数字之间找到回文数量。变量't'决定测试用例的数量。例如,如果t = 2,我将输入表示为:

2
12 23(in the next line)
56 78(in the next line)
Where the output would be:
1
2 (next line)

但输入后我的代码为:

2
12 23 

我得到输出为1然后我应该继续给出下一个测试用例

我如何才能同时提供两个测试用例?

import java.io.*;
import java.util.Scanner;
public class Checkall {
    public int check_all(int a, int b) {
        int c = 0;
        for (int y = a; y <= b; y++) {
            int z = y;
            int d = 0;
            while (z > 0) {
                d = d * 10 + z % 10;
                z /= 10;
            }
            if (d == y) c++;
        }
        return c;
    }
    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
        //System.out.println("Enter the limits");
        int t = Integer.parseInt(br.readLine());
        for (int s = 1; s <= t; s++) {
            Scanner scn;
            scn = new Scanner(System. in );
            int a = scn.nextInt();
            int b = scn.nextInt();
            Checkall p = new Checkall();
            p.check_all(a, b);
            System.out.print(p.check_all(a, b));
        }
    }
}

3 个答案:

答案 0 :(得分:2)

public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
    //System.out.println("Enter the limits");
    int t = Integer.parseInt(br.readLine());
    int[][] limits = new int[t][2];
    // 1st read all limits
    for (int s = 1; s <= t; s++) {
        Scanner scn;
        scn = new Scanner(System. in );
        limits[s-1][0] = scn.nextInt();
        limits[s-1][1] = scn.nextInt();
    }
    // 2nd process all of them.
    for ( int s = 1; s <= t; s++ ) {
        Checkall p = new Checkall();
        p.check_all(limits[s-1][0], limits[s-1][1]);
        System.out.print(p.check_all(limits[s-1][0], limits[s-1][1]));
    }
}

答案 1 :(得分:0)

为了调试这些格式问题,使用终端输入和输出并不是一个好主意。您可以将输入写入文件:

$ cat > in.txt
2
1 2
3 4
^C # press Control and C here

然后将其提供给您的程序:

$ java Checkall < in.txt

现在你只能看到输出了!

答案 2 :(得分:0)

读取所有值并将其添加到二维数组中。

int[][] values=new int[t][2]

在每次迭代中添加值

values[s][0]=first number
values[s][1]=second number

扫描迭代完成每个值集的调用计算方法