嵌套for循环偶数打印出来

时间:2017-10-24 03:34:57

标签: java loops nested

你好我正在编写一个代码,它会在第10个数字之后打印整数,它将在新行上开始,因此它将是每列10个数字和10行。我的代码打印所有数字,但我希望它只打印偶数我尝试使用if语句但它似乎没有工作,请帮助。下面是我的代码。

public static void mjj() {
    int s = 1;
    for (int i = 1; i <=10;i++) {
        for (int j = 1; j <=10;j++) {
            if (s % 2 == 0)
                System.out.print(s++ + " ");
        }
        System.out.println();
    }
}

输出

1 2 3 4 5 6 7 8 9 10 
11 12 13 14 15 16 17 18 19 20 
21 22 23 24 25 26 27 28 29 30 
31 32 33 34 35 36 37 38 39 40 
41 42 43 44 45 46 47 48 49 50 
51 52 53 54 55 56 57 58 59 60 
61 62 63 64 65 66 67 68 69 70 
71 72 73 74 75 76 77 78 79 80 
81 82 83 84 85 86 87 88 89 90 
91 92 93 94 95 96 97 98 99 100 

我想要打印这个

2 4 6 8 10 12 14 16 18 20 
22 24 26 28 30 32 34 36 38 40

等到第10行

6 个答案:

答案 0 :(得分:2)

我会改变

if (s % 2 == 0)
    System.out.print(s++ + " ");

System.out.printf("%4d", (2 * s++));

int格式化为四个空格,并将每个值乘以2(因此只显示偶数值)。

的一个小例子
   2   4   6   8  10  12  14  16  18  20
 ...
 182 184 186 188 190 192 194 196 198 200

答案 1 :(得分:2)

发布的代码不会打印任何内容,因为s永远不会更改。 您可以通过在s

之前移动if的增量来解决此问题
public static void mjj() {
    int s = 1;
    for (int i = 1; i <=10;i++) {
        for (int j = 1; j <=10;j++) {
            s++;  // <-- do it here
            if (s % 2 == 0)
                System.out.print(s + " ");
        }
        System.out.println();
    }
}

<强>输出

2 4 6 8 10 
12 14 16 18 20 
22 24 26 28 30 
32 34 36 38 40 
42 44 46 48 50 
52 54 56 58 60 
62 64 66 68 70 
72 74 76 78 80 
82 84 86 88 90 
92 94 96 98 100 

如果每行需要10个数字,请修改for循环:

public static void mjj() {
    int s = 1;
    for (int i = 1; i <= 5;i++) { // up to 5
        for (int j = 1; j <=20; j++) { // up to 20
            s++;  // <-- do it here
            if (s % 2 == 0)
                System.out.print(s + " ");
        }
        System.out.println();
    }
}

或者让它更简单:

public static void mjj() {
    for (int j = 2; j <=100; j += 2) {
        System.out.print(j + " ");
        if (j % 20 == 0)
            System.out.println();
    }
}

<强>输出

2 4 6 8 10 12 14 16 18 20 
22 24 26 28 30 32 34 36 38 40 
42 44 46 48 50 52 54 56 58 60 
62 64 66 68 70 72 74 76 78 80 
82 84 86 88 90 92 94 96 98 100 

答案 2 :(得分:0)

您的问题是,您在s中递增了System.out.print()。我认为你在你说过你试过的if语句中包围了这个,并且因为s从未满足条件,所以它从未打印过。尝试更像这样的东西:

int s = 1;
for (int i = 1; i <= 10;i++) 
{
   for (int j = 1; j <= 10;j++) 
   {
      s++;
      if (s % 2 == 0)
      {
         System.out.print(s + " ");
      }
    }
    System.out.println();
 }

答案 3 :(得分:0)

对我来说,这就是我在代码中会改变的内容。

public static void mjj() {
    int s = 0;
    for (int i = 1; i <=10;i++) {
        for (int j = 1; j <=10;j++) {
            s= s + 2;
            System.out.print(s + " ");
        }
        System.out.println();
    }
}

答案 4 :(得分:0)

以下是O(n)解决您问题的方法。

public static void main(String[] args) {
        int count = 0;
        for(int i=1;i<=100;i++) {
            if(i%2==0) {
                System.out.print(i+" ");
            }
            count++;
            if(count%20==0)
                System.out.println();
        }
    }

输出:

2 4 6 8 10 12 14 16 18 20 
22 24 26 28 30 32 34 36 38 40 
42 44 46 48 50 52 54 56 58 60 
62 64 66 68 70 72 74 76 78 80 
82 84 86 88 90 92 94 96 98 100 

答案 5 :(得分:0)

public static void mjj() {
            int s = 2;
            for (int i = 1; i <=10;i++) {
                for (int j = 1; j <=10;j++) {
                    if (s % 2 == 0){
                        System.out.print(s + " ");                      
                    }
                    s += 2;
                }
                System.out.println();
            }
    }
}

这将打印出来:

2 4 6 8 10 12 14 16 18 20 
22 24 26 28 30 32 34 36 38 40 
42 44 46 48 50 52 54 56 58 60 
62 64 66 68 70 72 74 76 78 80 
82 84 86 88 90 92 94 96 98 100 
102 104 106 108 110 112 114 116 118 120 
122 124 126 128 130 132 134 136 138 140 
142 144 146 148 150 152 154 156 158 160 
162 164 166 168 170 172 174 176 178 180 
182 184 186 188 190 192 194 196 198 200 

此外,您可以使用printf进行更好的打印输出:

public static void mjj() {
    int s = 2;
    for (int i = 1; i <= 10; i++) {
        for (int j = 1; j <= 10; j++) {
            if (s % 2 == 0) {
                System.out.printf("%3s ", s );
            }
            s += 2;
        }
        System.out.println();
    }
}

这将打印出来:

  2   4   6   8  10  12  14  16  18  20 
 22  24  26  28  30  32  34  36  38  40 
 42  44  46  48  50  52  54  56  58  60 
 62  64  66  68  70  72  74  76  78  80 
 82  84  86  88  90  92  94  96  98 100 
102 104 106 108 110 112 114 116 118 120 
122 124 126 128 130 132 134 136 138 140 
142 144 146 148 150 152 154 156 158 160 
162 164 166 168 170 172 174 176 178 180 
182 184 186 188 190 192 194 196 198 200 
相关问题