Closed. This question needs debugging details。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                    
                
        

我有一个代码创建并填充数组,然后将其转换为列表。

    // I made a scanner and an integer "input".
    Scanner in = new Scanner(System.in);
    int input = in.nextInt();
    // I make input into an array
    int[] ints = new int[input];

    // I fill the array
    for(int i = 0; i < ints.length; i++){
        ints[i] = i + 1;
    }

    // transform it into a list
    List<Integer> intList = new ArrayList<Integer>();
    for (int index = 0; index < ints.length; index++)
    {
        intList.add(ints[index]);
    }


现在,我想输出其索引不可被2除的所有System.out.println();项目(具有索引的项目:1 3 5,依此类推)。

最佳答案

不能被2整除num % 2 != 0,因此您可以执行以下操作:

if(i % 2 != 0) {
    System.out.println(whatever);
}

关于java - 如果不能被2整除,则从列表中输出项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28506756/

10-09 01:51