Java 嵌套循环偶数打印输出

Java 嵌套循环偶数打印输出,java,loops,nested,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

您好,我正在编写一个代码,它将打印第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();
    }
}
我想让它打印这个

2 4 6 8 10 12 14 16 18 20 
22 24 26 28 30 32 34 36 38 40
依此类推,直到第十排,我会改变

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

int
格式化为四个空格,并将每个值乘以
2
(因此仅显示偶数值)。其中一个小例子是

   2   4   6   8  10  12  14  16  18  20
 ...
 182 184 186 188 190 192 194 196 198 200

发布的代码不会打印任何内容,因为
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();
    }
}
如果希望每行有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();
    }
}

您的问题是在
System.out.print()
中递增
s
。我想你在你说你试过的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();
 }
ints=1;

对于(inti=1;i对于我来说,这是我将在代码中更改的内容

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();
    }
}
publicstaticvoidmjj(){
int s=0;
对于(inti=1;i,这里是问题的
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();
        }
    }
此外,您还可以使用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();
    }
}

把你的
if
语句放回原处,这样我们就可以看到它有什么问题了。目前甚至没有任何尝试只打印evens。话虽如此,你甚至可能不需要
if
。@John3136我在代码中添加了if语句,不打印任何内容:你不必将循环计数器步进1使用
j++
;您可以使用
j++=2
j=j+2
以2为单位进行计算。“s”是如何递增的?它从1和1%2开始!=0,所以当开始执行++s时?@johnmac您的解决方案是
O(10n)
,这对于n行打印的行数来说是最差的。我添加了一个
O(n)
您问题的解决方案。它将迭代次数减少到一半。零是偶数。对于粗心的错误,我深表歉意。如果(s%2!=0)s=s++System.out.print(s+“”),可能可以使用s=s+2;在
向s添加2之前,您可以只打印
s
。OP希望一列中有10个数字。您仍然认为您的解决方案是最佳的吗?对于一个外部循环,内部循环将执行20次。这是通过查看您的解决方案所发生的情况。是的,我得到了它,但它仍然像
O(10n)
哪一个更糟糕。@Lokesh AFAIK O(10n)=O(kn)==O(n),其中
k
是一个常数。嵌套循环不会自动生成
O(n^2)
解决方案。原始解决方案最差
O(10n)
对于
n
打印的行数。@KevinAnderson查看OP解决方案,内部循环将对一个外部循环执行10次。这使得对10个外部循环执行100次内部循环是的,但是如果将外部循环的步数加倍,内部循环仍然只对每个外部循环迭代执行10次启用,则总步数只会翻倍,而不是翻倍。因此,外循环迭代次数的线性时间复杂度
O(10n)
。但是,如果两个循环都绑定到相同的因子(即,如果外循环翻倍,内循环也总是翻倍),则它确实会变成
O(n^2)
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 
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();
 }
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();
    }
}
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 
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 
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