英文:
In Java, how would I code a pyramid of asterisks by using nested for loops?
问题
我正在处理一个作业,需要使用嵌套循环编写一个侧倾的星号金字塔程序。
这个程序的输出应该类似这样:
**
**
*
当我运行我的程序时,它只显示代码的最后四行。我不知道前面的三行为什么没有显示出来。
这是我的代码:
public class Main
{
public static void main(String[] args) {
for(int a = 0; a < 8; a++) //1
{
if(a < 4){
for(int b = a; b < 4; b++)
{
System.out.print("*");
}
}
if(a >= 4)
for(int c = a; c < 4; c++)
{
System.out.print("*");
}
System.out.println();
} //loop 1
}
}
这是我的输出:
**
*
(输出后面有一些空格,我没有包含在内。这是因为外部循环迭代了八次。)如何使我的程序正确显示所有代码,而不仅仅是最后四行?
非常感谢您的帮助。
英文:
I am working on an assignment where I have to use nested loops to program a pyramid of asterisks that is on its side.
The output of this program should look like this:
*
**
***
****
***
**
*
When I run my program, it only displays the final four lines of the code. I don't know why the first three don't show up.
Here is my code:
public class Main
{
public static void main(String[] args) {
for(int a = 0; a < 8; a++) //1
{
if(a < 4){
for(int b = a; b < 4; b++)
{
System.out.print("*");
}
}
if(a >= 4)
for(int c = a; c < 4; c++)
{
System.out.print("*");
}
System.out.println();
} //loop 1
}
}
And here is my output:
****
***
**
*
(There is some empty space after the output that I did not include. This is caused by the outer for loop iterating eight times.) How would I make my program correctly display all of the code, instead of just the last four lines?
Any help would be greatly appreciated.
答案1
得分: 1
在你的逻辑中有几个错误:
- 由于你只需要 7 行,第一个循环应该迭代直到 a < 7
- 在前 3 行中,你的嵌套循环应该从 0 迭代到 a
- 此后,另一个嵌套循环应该从 a 迭代到 7
- 最好使用 if-else 语句替代两个 if 语句
以下是我测试过的完整解决方案:
for (int a = 0; a < 7; a++) {
if (a < 4) {
for (int b = 0; b <= a; b++)
System.out.print("*");
} else {
for (int c = a; c < 7; c++)
System.out.print("*");
}
System.out.println();
}
输出:
*
**
***
****
***
**
*
编辑:
正如在评论中提到的,你还可以将外部循环拆分成两部分,以消除条件,代码如下:
for (int a = 0; a < 4; a++) {
for (int b = 0; b <= a; b++)
System.out.print("*");
System.out.println();
}
for (int a = 4; a <= 7; a++) {
for (int b = a; b < 7; b++)
System.out.print("*");
System.out.println();
}
英文:
There are several mistakes in your logic:
- Since you only need 7
rows
, the first loop should iterate untila < 7
- In the first 3 rows, your
nested loop
should iterate from0
toa
- After that, the other
nested loop
should go froma
to7
- It is better to use
if-else
instead of twoif
statements
Here is the complete solution that I tested:
for(int a = 0; a < 7; a++) {
if(a < 4){
for(int b = 0; b <= a; b++)
System.out.print("*");
}else {
for(int c = a; c < 7; c++)
System.out.print("*");
}
System.out.println();
}
Output:
*
**
***
****
***
**
*
EDIT:
As mentioned in the comments, you can also split the outer loop into two parts in order to remove the conditions as follows:
for(int a = 0; a < 4; a++) {
for(int b = 0; b <= a; b++)
System.out.print("*");
System.out.println();
}
for(int a = 4; a <= 7; a++) {
for(int b = a; b < 7; b++)
System.out.print("*");
System.out.println();
}
答案2
得分: 0
你很接近了。尝试类似这样的代码:
int size = 4;
for (int line = 1; line < size * 2; line++) {
if (line <= size) {
for (int i = 0; i < line; i++) {
System.out.print("*");
}
} else {
for (int i = 0; i < size * 2 - line; i++) {
System.out.print("*");
}
}
System.out.println();
}
英文:
You're close. Try something like this:
int size = 4;
for(int line = 1; line < size * 2; line++) {
if(line <= size) {
for(int i = 0; i < line; i++) {
System.out.print("*");
}
}
else {
for(int i = 0; i < size * 2 - line; i++) {
System.out.print("*");
}
}
System.out.println();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论