繁体   English   中英

Java迭代项目问题

[英]Java iteration project issue

我需要做这件事来做家庭作业,但我似乎无法使它正常工作...应该做的是输出以下内容:

**********
*********
********
*******
******
*****
****
***
**
*

这是我的代码:

public class stars {

    public static void main(String args[]){

        for(int l = 1; l<= 10; l++){
            System.out.println();
            for(int i = 10; i>= 1; i--){
                System.out.print("*");
            }
        }

    }

}

这似乎输出:

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

我希望有人能帮助我! 谢谢!

for(int l = 1; l<= 10; l++){
    System.out.println();
    for(int i = 10; i>= 1; i--){
        System.out.print("*");
    }
}

您需要将inner loop的终止条件从i >= 1更改为i >= l ,否则每次迭代将运行10 times

for(int l = 1; l<= 10; l++){
    System.out.println();
    for(int i = 10; i>= l; i--){
        System.out.print("*");
    }
}

并且,请不要使用以下名称来命名变量: l看起来像One ,类似地, O看起来像Zero

如我上面的评论所述:

for(int l = 1; l<= 10; l++){
    System.out.println();
    for(int i = l + 1; i <= 10; i++){
        System.out.print("*");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM