Java笔试程序(一)——按行读取数据

1.读取一行数字:

public class juZhen {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String[] str = null;
        str = cin.nextLine().split(" ");
        int a[] = new int[str.length];
        for(int i=0;i) {
            a[i] = Integer.valueOf(str[i]);
        }
        System.out.println(Arrays.toString(a));//注意这里对数组a的输出格式
    }
}

2.读取两行数字,读取多行数据与之类似:

public class juZhen {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        
        String[] s1 = null;
        s1 = cin.nextLine().split(" ");
        int[] a1 = new int[s1.length];
        for(int i=0;i) {
            a1[i] = Integer.valueOf(s1[i]);
        }

        String[] s2 = null;
        s2 = cin.nextLine().split(" ");
        int[] a2 = new int[s2.length];
        for(int i=0;i) {
            a2[i] = Integer.valueOf(s2[i]);
        }
        System.out.println(Arrays.toString(a1));
        System.out.println(Arrays.toString(a2));        
    }    
}

 

你可能感兴趣的:(Java笔试程序(一)——按行读取数据)