gpt4 book ai didi

java - 每行打印 11 个数字?

转载 作者:行者123 更新时间:2023-12-01 10:44:50 26 4
gpt4 key购买 nike

我必须对著名的“FizzBu​​zz”代码进行变体,它会计算用户输入的数字,并且每行只打印 11 个数字。我的代码似乎有点工作,但它每行重复同样的事情 11 次。

有人可以帮我解决这个问题吗?

import javax.swing.JOptionPane;

public class CozaLoza2 {
public static void main(String[] args) {

String n = JOptionPane.showInputDialog("Please input an interger");
int result = Integer.parseInt(n);
for (int i = 0; i <= result; i++) {
for (int j = 0; j <= 10; j++) {

System.out.print(" ");

if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) {
System.out.print("CozaLozaWoza");
}

if (i % 3 == 0 && i % 5 == 0) {
System.out.print("CozaLoza");

} else if (i % 5 == 0) {
System.out.print("Loza");

} else if (i % 3 == 0) {
System.out.print("Coza");

} else if (i % 7 == 0) {
System.out.print("Woza");

} else {
System.out.print(" " + i);
}
}
}
System.out.println();
}
}

这是在该程序中输入数字“20”时得到的输出

CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 4 4 4 4 4 4 4 4 4 4 4 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza 8 8 8 8 8 8 8 8 8 8 8 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza 11 11 11 11 11 11 11 11 11 11 11 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 13 13 13 13 13 13 13 13 13 13 13 Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza 16 16 16 16 16 16 16 16 16 16 16 17 17 17 17 17 17 17 17 17 17 17 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 19 19 19 19 19 19 19 19 19 19 19 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza

最佳答案

我很高兴地宣布,你自己的缺乏缩进是你的失败。这乱七八糟的

  }
}
}
System.out.println();
}
}

应该是这样困惑:

  }
}
System.out.println();
}
}
}

现在去学习如何缩进和修复缩进。

另外,在外循环中for (int i = 0; i <= result; i++)您正在运行result + 1值(value)观。也许您需要i < result .

(以防万一问题被编辑,我说的是原始问题。)

关于java - 每行打印 11 个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34249097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com