Java怎么根据第一个数字n,然后决定读取以下n行数据?

举个例子要求输入是:
3
1 4
3 4
1 2
4 1

这怎么根据3就能读入前三行的数据呢,而不会读入4 1这一行?

import java.util.*;
import java.io.*;
public class Yugi{
    public static void main(String[] args) throws Exception{
        Scanner scan = new Scanner(System.in);
        System.out.println("决定读取以下n行数据?");
        int n = scan.nextInt();
        scan  = new Scanner(new File("data.txt"));
        int i = 1;
        while(i <= n && scan.hasNextLine()){
            System.out.println(scan.nextLine());
            i++;
        }
        scan.close();
    }
}

温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜