通过扫描仪读取用户输入的最佳方式是什么?

What is the best way to read user inputs via scanner?

我在 Java 中使用了一些类似于以下方法的方法:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int[] a= new int[3];
    
    //assign inputs
    for (int i=0;i<3;i++)
        a[i] = scan.nextInt();
    scan.close();
    
    //print inputs
    for(int j=0;j<3;j++)
        System.out.println(a[j]);
}

然而,一般第一个输入参数是长度,因此我使用额外的计数器(c)来区分第一个元素。使用这种方法时,扫描器不会一个一个地读取输入,并且检查两个块中的第一个和其他元素似乎是多余的。

// input format: size of 3 and these elements (4, 5, 6)
// 3
// 4 5 6    
public static void getInput() {
    int n = 0; //size
    int c = 0; //counter for distinguish the first index
    int sum = 0; //
    int[] array = null;
    
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter size:");

    //block I: check the first element (size of array) and assign it
    if (scan.nextInt() <= 0)
        System.out.println("n value must be greater than 0");
    else {
        n = scan.nextInt();
        array = new int[n];
    }

    System.out.println("Enter array elements:");
    
    //block II: check the other elements adn assign them
    while(scan.hasNextInt() && c<n) {
        if (scan.nextInt() >= 100) {
            System.out.println("Array elements must be lower than 100");
        } else {
            array[c] = scan.nextInt();
            c++;
        }       
    }
    scan.close();

    int sum = 0;
    for (int j = 0; j < n; j++) {
        sum += array[j];
    }
    System.out.println("Sum = " + sum);
}

我的问题是“如何用单个块修改此方法(whileforwhile 内循环)?我尝试了 5-6 种不同的变体,但 [=其中 22=] 个正常工作?

希望对您有所帮助,

   public static void getInput() {
        int n; //size
        int c = 0; //counter for distinguish the first index
        int sum = 0; //
        int[] array;

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter size:");

        //check the first element (size of array) and assign it
        n = scan.nextInt();
        while(n <= 0)
        {
            System.out.println("n value must be greater than 0");
            System.out.println("Enter size:");
            n = scan.nextInt();
        }

        array = new int[n];

        System.out.println("Enter array elements:");

        // check the other elements and assign them
        while(c<n) {
            int num = scan.nextInt();
            if (num >= 100) {
                System.out.println("Array elements must be lower than 100");
            } else {
                array[c++] = num;
            }
        }
        scan.close();

        for (int j = 0; j < n; j++) {
            sum += array[j];
        }
        System.out.println("Sum = " + sum);
    }

输出:

Enter size:
-1
n value must be greater than 0
Enter size:
4
Enter array elements:
1
2
3
4
Sum = 10

我已经优化了您的代码并修复了一些错误。

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter size:");

        int n = scan.nextInt();
        if (n <= 0) {
            System.out.println("n value must be greater than 0");
            return; // need to break from here
        }

        int c = 0;
        int sum = 0; // no need for array
        System.out.println("Enter array elements:");
        // check the other elements and assign them
        while (c++ < n) {
            int next = scan.nextInt();
            if (next >= 100) {
                System.out.println("Array elements must be lower than 100");
                c--; // ensure can reenter the number
            } else {
                sum += next; 
            }
        }
        scan.close();

        System.out.println("Sum = " + sum);
    }