我的三角形打印了两次。怎么解决?

huangapple go评论59阅读模式
英文:

My triangle printed out twice. How to solve it?

问题

package javaapplication4;

public class NewClass6 {
  public static void main(String[] args) {
    for (int i = 1; i <= 9; i++) {
      int sum = 1;
      for (int j = 1; j <= i; j++) {
        if (j % 2 != 0) {
          System.out.print(j);
          sum *= j;
        }
        if (j == i)
          System.out.print("=");
        else if (j % 2 != 0)
          System.out.print("*");
      }
      System.out.println(sum);
    }
  }
}
英文:

It printing out * and = twice

I'm making a sequence triangle (again), now with odd numbers only and multiplying.
It appear to be printing * and = twice.

The result should be like this:
1 = 1

1 * 3=3

1* 3* 5=15

etc

package javaapplication4;

public class NewClass6 {
  public static void main(String[] args) {
    for (int i = 1; i &lt;= 9; i++) {
      int sum = 1;
      for (int j = 1; j &lt;= i; j++) {
        if (j % 2 != 0) {
          System.out.print(j);
          sum *= j;
        }
        if (j == i)
          System.out.print(&quot;=&quot;);
        else if (j % 2 != 0)
          System.out.print(&quot;*&quot;);
      }
      System.out.println(sum);
    }
  }
}

答案1

得分: 3

这是因为您在循环中将 i1 ~ 9 循环,而将 j1~i 循环,每次只增加1。

需要循环的是奇数,所以每次增加2。
因此代码应该是:

package javaapplication4;

public class NewClass6 {
  public static void main(String[] args) {
    for (int i = 1; i <= 9; i += 2) {
      int sum = 1;
      for (int j = 1; j <= i; j += 2) {
        if (j % 2 != 0) {
          System.out.print(j);
          sum *= j;
        }
        if (j == i)
          System.out.print("=");
        else if (j % 2 != 0)
          System.out.print("*");
      }
      System.out.println(sum);
    }
  }
}
英文:

This happens because you have looped i from 1 ~ 9 and j from 1~i by creasing only one per step.

It is needed to loop only odd numbers so increase 2 per step.
So for (int i = 1; i &lt;= 9; i +=2) {

package javaapplication4;

public class NewClass6 {
  public static void main(String[] args) {
    for (int i = 1; i &lt;= 9; i+=2) {
      int sum = 1;
      for (int j = 1; j &lt;= i; j+=2) {
        if (j % 2 != 0) {
          System.out.print(j);
          sum *= j;
        }
        if (j == i)
          System.out.print(&quot;=&quot;);
        else if (j % 2 != 0)
          System.out.print(&quot;*&quot;);
      }
      System.out.println(sum);
    }
  }
}

答案2

得分: 3

简洁起见

    StringBuilder buf = new StringBuilder();

    for (int i = 1, mul = i; i <= 9; i += 2) {
        if (buf.length() > 0)
            buf.append('*');
    
        buf.append(i);
        mul *= i;
    
        System.out.format("%s=%d\n", buf, mul);
    }

**输出**

    1=1
    1*3=3
    1*3*5=15
    1*3*5*7=105
    1*3*5*7*9=945
英文:

Be simple!

StringBuilder buf = new StringBuilder();

for (int i = 1, mul = i; i &lt;= 9; i += 2) {
    if (buf.length() &gt; 0)
        buf.append(&#39;*&#39;);

    buf.append(i);
    mul *= i;

    System.out.format(&quot;%s=%d\n&quot;, buf, mul);
}

Output:

1=1
1*3=3
1*3*5=15
1*3*5*7=105
1*3*5*7*9=945

答案3

得分: 2

以下是翻译后的代码部分:

第一段代码:

for (int i = 1; i <= 9; i++) {
    if (i % 2 != 0) {
        int sum = 1;
        for (int j = 1; j <= i ; j++) {
            if(j % 2 != 0)
            {
                System.out.print(j);
                sum *= j; 
                if (j == i) 
                    System.out.print("=");
                else if (j % 2 != 0)
                    System.out.print("*");    
            }
        }
        System.out.println(sum);
    }
}

第二段代码:

for (int i = 1; i <= 9; i += 2) {
    int sum = 1;
    for (int j = 1; j <= i ; j += 2) {
        System.out.print(j);
        sum *= j; 
        if (j == i) 
            System.out.print("=");
        else if (j % 2 != 0)
            System.out.print("*");    
    }
    System.out.println(sum);
}
英文:

You're considering odd numbers in internal loop but you're not doing so in outer loop, either skip even numbers by changing i++ to i += 2 or add another if block for checking if i is not even:

for (int i = 1; i &lt;= 9; i++) {
    if (i % 2 != 0) { // Skipping outer loop&#39;s even numbers
        int sum = 1;
        for (int j = 1; j &lt;= i ; j++) {
            if(j % 2 != 0)
            {
                System.out.print(j);
                sum *= j; 
                if (j == i) 
                    System.out.print(&quot;=&quot;);
                else if (j % 2 != 0)
                    System.out.print(&quot;*&quot;);    
            }

        }

        System.out.println(sum);
    }
}

Or skip even elements by += 2 rather than ++(I would suggest using this)

for (int i = 1; i &lt;= 9; i += 2) {
    int sum = 1;
    for (int j = 1; j &lt;= i ; j += 2) {
        System.out.print(j);
        sum *= j; 
        if (j == i) 
            System.out.print(&quot;=&quot;);
        else if (j % 2 != 0)
            System.out.print(&quot;*&quot;);    
    }
    System.out.println(sum);
}

huangapple
  • 本文由 发表于 2020年10月16日 23:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定