当扫描仪位于输入端时如何使循环停止

时间:2018-09-03 23:29:46

标签: java arrays arraylist

我目前正在尝试制作一个程序,该程序将允许用户输入任意数量的整数(我现在只是要求他们输入9作为测试),并让rotateArray函数旋转整数数组。例如:

输入:1 2 3 4 5 输出:5 4 3 2 1

之所以要包含arraylist是因为我想让程序动态分配内存,以便用户也可以输入尽可能多的单位输入。我的问题是我当前正在使用的for循环。我正在寻找一种适当的方法,使for循环在到达用户输入的末尾时停止。我尝试使用scan.nextInt().isEmpty(),但是没有按预期工作。

 public static void main(String[] args) 

    {
        Scanner scan = new Scanner(System.in);

        System.out.println("This program takes two arrays, compares them, and "
        + "determines whether the original array has been rotated and put "
        + "into another array. \nWatch what happens when the original "
        + "array = [0,1,2,3,4,5,6,7,8,9] is compared to an array  with contents: \n"
        + "[9,7,5,3,1,8,6,4,2,0]");

        int[] original = {0,1,2,3,4,5,6,7,8,9};
        int[] notRotated = {9,7,5,3,1,8,6,4,2,0};
        int[] rotatedArray = {9,8,7,6,5,4,3,2,1,0}; 
        boolean rotation;      

        rotation = isRotated(original, rotatedArray);
        if(rotation == true)
        {
            System.out.println("The original array has been rotated!");
        }else{
            System.out.println("The original array has not been rotated");
        }

        System.out.println("\n Watch what happens when the original array is compared to an array"
                         + " with contents \n [9,8,7,6,5,4,3,2,1,0]");

        rotation = isRotated(original, rotatedArray);
        if(rotation == true)
        {
            System.out.println("The original array has been rotated!");
        }else{
            System.out.println("The original array has not been rotated");
        }

        ArrayList<Integer> userArray = new ArrayList<Integer>(9);

        System.out.println("This program can also rotate arrays that contain "
                         + "single digit integers.\n Enter 9 single digit "
                         + "integers separated by spaces");

        //*****************************************************
        userArray.add(scan.nextInt());
        for(int i = 0; i<userArray.size(); i++)
        {
            //*****problem
            if(???????? )
                break;
            else         
                userArray.add(scan.nextInt());
        }

        System.out.println("The array you entered is: " + userArray.toString() +"\n");

        rotateArray(userArray);

        System.out.println("When your array is rotated, it looks like this: \n" + 
                           userArray.toString());

    }

public static ArrayList<Integer> rotateArray(ArrayList<Integer> userArray)
    {    
        int replace = 0;
        int inc = 1;
        int indexVariable = 0;

        //if number of elements equals an even number
        if(userArray.size() % 2 == 0)
        {
            for(int i = 0; i < (userArray.size()/2);i++)
            {
                replace = userArray.get(i);                             
                userArray.set(userArray.get(i),userArray.size() - inc );
                userArray.set(userArray.size() - inc, replace);
                inc++;
            }
        }

        //if number of elements equals an odd number
        else
        {
            for (int i = 0; i <(userArray.size()/2) ; i++) 
            {
                replace = userArray.get(i);                             
                userArray.set(userArray.get(i),userArray.size() - inc );
                userArray.set(userArray.size() - inc, replace);
                inc++;
            }
        }

        return userArray;

    }  

2 个答案:

答案 0 :(得分:0)

关于扫描仪的问题是,#hasNext从控制台读取时,仅在关闭扫描仪时(例如,当您关闭扫描仪或控制台不再可用于输入时)才返回false。否则,调用它会告诉扫描仪等待输入,并告知您输入是否有效(例如,如果您调用#hasNextInt)。

因此,IMO解决您问题的最佳方法是将扫描仪读取为字符串,然后将其拆分并按以下方式自行处理。

String input=scan.nextLine();
    String[] numbers=input.split(" ");
    for(String number:numbers)
    {
        if(number.isEmpty())
            continue;//check for trailing so input like 3   4   5 is read
        userArray.add(Integer.parseInt(number));//You would want to add a catch here for invalid input.
    }

答案 1 :(得分:0)

如果input: 1 2 3 4 5userArray.size应该匹配original.length,那么您可以这样做:

int size = original.length;
for (int i = 0; i < size; i++) {
            int num = scan.nextInt();
            userArray.add(num);

或者您可以对变量size进行硬编码:

int size = 9;

或从控制台输入它:

int size = scan.nextInt();