无法获得循环打印出最多237的数字

时间:2014-09-25 16:08:30

标签: java netbeans

//Loop through and print out all even numbers from the numbers list in the same order
//they are received. Don't print any numbers that come after 237 in the sequence.
public class excercise4 {

    public static void main(String[] args) {
        int[] numbers = {
            951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 
            615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 
            386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
            399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
            815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
            958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 
            743, 527};


        for (int i=0; i < numbers.length; i++) {


            int el = numbers[i];
            if (el >= 237) break;
            if (el%2==0)           // using the remainder operator %
            System.out.print(el+" ");  // the Expected Output likes it like this!
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可能想要更改此行:

if (el >= 237) break;

这个

if (el >= 237) continue;

因为break句子会从整个循环中消失。虽然continue将转到数组的 next 元素。

答案 1 :(得分:1)

而不是:

if (el >= 237) break;

有:

if (el >= 237) continue;