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

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

My triangle printed out twice. How to solve it?

问题

  1. package javaapplication4;
  2. public class NewClass6 {
  3. public static void main(String[] args) {
  4. for (int i = 1; i <= 9; i++) {
  5. int sum = 1;
  6. for (int j = 1; j <= i; j++) {
  7. if (j % 2 != 0) {
  8. System.out.print(j);
  9. sum *= j;
  10. }
  11. if (j == i)
  12. System.out.print("=");
  13. else if (j % 2 != 0)
  14. System.out.print("*");
  15. }
  16. System.out.println(sum);
  17. }
  18. }
  19. }
英文:

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

  1. package javaapplication4;
  2. public class NewClass6 {
  3. public static void main(String[] args) {
  4. for (int i = 1; i &lt;= 9; i++) {
  5. int sum = 1;
  6. for (int j = 1; j &lt;= i; j++) {
  7. if (j % 2 != 0) {
  8. System.out.print(j);
  9. sum *= j;
  10. }
  11. if (j == i)
  12. System.out.print(&quot;=&quot;);
  13. else if (j % 2 != 0)
  14. System.out.print(&quot;*&quot;);
  15. }
  16. System.out.println(sum);
  17. }
  18. }
  19. }

答案1

得分: 3

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

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

  1. package javaapplication4;
  2. public class NewClass6 {
  3. public static void main(String[] args) {
  4. for (int i = 1; i <= 9; i += 2) {
  5. int sum = 1;
  6. for (int j = 1; j <= i; j += 2) {
  7. if (j % 2 != 0) {
  8. System.out.print(j);
  9. sum *= j;
  10. }
  11. if (j == i)
  12. System.out.print("=");
  13. else if (j % 2 != 0)
  14. System.out.print("*");
  15. }
  16. System.out.println(sum);
  17. }
  18. }
  19. }
英文:

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) {

  1. package javaapplication4;
  2. public class NewClass6 {
  3. public static void main(String[] args) {
  4. for (int i = 1; i &lt;= 9; i+=2) {
  5. int sum = 1;
  6. for (int j = 1; j &lt;= i; j+=2) {
  7. if (j % 2 != 0) {
  8. System.out.print(j);
  9. sum *= j;
  10. }
  11. if (j == i)
  12. System.out.print(&quot;=&quot;);
  13. else if (j % 2 != 0)
  14. System.out.print(&quot;*&quot;);
  15. }
  16. System.out.println(sum);
  17. }
  18. }
  19. }

答案2

得分: 3

  1. 简洁起见
  2. StringBuilder buf = new StringBuilder();
  3. for (int i = 1, mul = i; i <= 9; i += 2) {
  4. if (buf.length() > 0)
  5. buf.append('*');
  6. buf.append(i);
  7. mul *= i;
  8. System.out.format("%s=%d\n", buf, mul);
  9. }
  10. **输出**
  11. 1=1
  12. 1*3=3
  13. 1*3*5=15
  14. 1*3*5*7=105
  15. 1*3*5*7*9=945
英文:

Be simple!

  1. StringBuilder buf = new StringBuilder();
  2. for (int i = 1, mul = i; i &lt;= 9; i += 2) {
  3. if (buf.length() &gt; 0)
  4. buf.append(&#39;*&#39;);
  5. buf.append(i);
  6. mul *= i;
  7. System.out.format(&quot;%s=%d\n&quot;, buf, mul);
  8. }

Output:

  1. 1=1
  2. 1*3=3
  3. 1*3*5=15
  4. 1*3*5*7=105
  5. 1*3*5*7*9=945

答案3

得分: 2

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

第一段代码:

  1. for (int i = 1; i <= 9; i++) {
  2. if (i % 2 != 0) {
  3. int sum = 1;
  4. for (int j = 1; j <= i ; j++) {
  5. if(j % 2 != 0)
  6. {
  7. System.out.print(j);
  8. sum *= j;
  9. if (j == i)
  10. System.out.print("=");
  11. else if (j % 2 != 0)
  12. System.out.print("*");
  13. }
  14. }
  15. System.out.println(sum);
  16. }
  17. }

第二段代码:

  1. for (int i = 1; i <= 9; i += 2) {
  2. int sum = 1;
  3. for (int j = 1; j <= i ; j += 2) {
  4. System.out.print(j);
  5. sum *= j;
  6. if (j == i)
  7. System.out.print("=");
  8. else if (j % 2 != 0)
  9. System.out.print("*");
  10. }
  11. System.out.println(sum);
  12. }
英文:

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:

  1. for (int i = 1; i &lt;= 9; i++) {
  2. if (i % 2 != 0) { // Skipping outer loop&#39;s even numbers
  3. int sum = 1;
  4. for (int j = 1; j &lt;= i ; j++) {
  5. if(j % 2 != 0)
  6. {
  7. System.out.print(j);
  8. sum *= j;
  9. if (j == i)
  10. System.out.print(&quot;=&quot;);
  11. else if (j % 2 != 0)
  12. System.out.print(&quot;*&quot;);
  13. }
  14. }
  15. System.out.println(sum);
  16. }
  17. }

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

  1. for (int i = 1; i &lt;= 9; i += 2) {
  2. int sum = 1;
  3. for (int j = 1; j &lt;= i ; j += 2) {
  4. System.out.print(j);
  5. sum *= j;
  6. if (j == i)
  7. System.out.print(&quot;=&quot;);
  8. else if (j % 2 != 0)
  9. System.out.print(&quot;*&quot;);
  10. }
  11. System.out.println(sum);
  12. }

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:

确定