ZZULIOJ 1111: 多个整数的逆序输出(JAVA) ,为什么在提交的时候会出现运行时错误?

ZZULIOJ 1111: 多个整数的逆序输出(JAVA) ,为什么在提交的时候会出现运行时错误?

问题描述:

import java.util.Scanner;

public class Main {
    void inverse(int n){
        Scanner s = new Scanner(System.in);
        int a ;
        if(n>1){
             a= s.nextInt();
            inverse(n-1);
            System.out.print(a+" ");
        }
        if(n==1) {
            a= s.nextInt();
            System.out.print(a + " ");
        }
    }

    public static void main(String[] args) {
        Main o = new Main();
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        o.inverse(n);
    }
}


你main函数里面和inverse用的不是一个scanner,所以面对11 22 33 44 55这样的输入只会读取到11,你可以给inverse函数加个scanner的参数,然后把main的scanner传进去