简体   繁体   English

For循环中的多个扫描仪输入

[英]Multiple Scanner Input In For Loop

I've just begun to learn how to program with Java and I had a question with regard to the scanner input. 我刚开始学习如何用Java编程,我对扫描仪输入有疑问。 I'm building a little program that simply asks the user for input to create a numerical array. 我正在构建一个小程序,它只是要求用户输入以创建数值数组。 I was wondering if there was a way to check for the numerical input encompassing the for loop, instead of putting a while check on each of my cases in the for loop. 我想知道是否有办法检查包含for循环的数字输入,而不是在for循环中对我的每个案例进行一次检查。

As well, any other comments or suggestions on my code to help me improve and understand what I am doing would be greatly appreciated! 同样,我的代码的任何其他意见或建议,以帮助我改善和理解我在做什么将不胜感激!

Thank you! 谢谢!

Edit: I'm calling this class from a 'Main' class where I run the program. 编辑:我从运行该程序的'Main'类调用此类。

import java.util.Scanner; //Import the use of the Java Scanner

public class ArrayBuild { // Open Application

    private static Scanner input;

    public Double[] anArray;

    public static int arrayCount = 0;

    public ArrayBuild() { // Constructor for ArrayBuild object

        input = new Scanner(System.in);

        arrayCount++;

        System.out.println("This will be Array: " + arrayCount);

        // Array Size Declaration
        System.out.println("Enter Array Size: ");
        while (!input.hasNextInt()) {
            System.out.println("Please enter an integer for Array size!");
            input.next();
        }
        int n = input.nextInt();
        anArray = new Double[n]; // Create 'anArray' of size n
        //

        for (int i = 0; i < n; i++) { // Begin For Loop

            if (i == 0) {
                System.out.println("Enter First Number: ");
                while (!input.hasNextDouble()) {
                    System.out.println("Please enter a number for array data!");
                    input.next();
                }
                Double D = input.nextDouble();
                anArray[i] = D;
            }

            else if (i > 0 && i < (n - 1)) {
                System.out.println("Enter Next Number: \n");
                while (!input.hasNextDouble()) {
                    System.out.println("Please enter a number for array data!");
                    input.next();
                }
                Double D = input.nextDouble();
                anArray[i] = D;
            }

            else if (i == (n - 1)) {
                System.out.println("Enter Final Number: ");
                while (!input.hasNextDouble()) {
                    System.out.println("Please enter a number for array data!");
                    input.next();
                }
                Double D = input.nextDouble();
                anArray[i] = D;
            }
        } // End For Loop
    }
} // Close Class

One thing you can do to simplify and write clean code is to always separate the repeating code. 简化和编写干净代码可以做的一件事就是始终将重复代码分开。 In your case, inside the for loop, you are only changing the print statement inside the if condition. 在您的情况下,在for循环中,您只是在if条件中更改print语句。 Take the other code outside like this-- 像这样拿外面的其他代码 -

for (int i = 0; i < n; i++) { // Begin For Loop

        if (i == 0) 
            System.out.println("Enter First Number: ");

        else if (i > 0 && i < (n - 1))
            System.out.println("Enter Next Number: \n");

        else if (i == (n - 1)) 
            System.out.println("Enter Final Number: ");

        while (!input.hasNextDouble()) {
                System.out.println("Please enter a number for array data!");
                input.next();
        }
        Double D = input.nextDouble();
        anArray[i] = D;

    } // End For Loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM