英文:
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);
}
}
}
英文:
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 <= 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);
}
}
}
答案1
得分: 3
这是因为您在循环中将 i
从 1 ~ 9
循环,而将 j
从 1~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 <= 9; i +=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);
}
}
}
答案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 <= 9; i += 2) {
if (buf.length() > 0)
buf.append('*');
buf.append(i);
mul *= i;
System.out.format("%s=%d\n", 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 <= 9; i++) {
if (i % 2 != 0) { // Skipping outer loop's even numbers
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);
}
}
Or skip even elements by += 2
rather than ++
(I would suggest using this)
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论