为了账号安全,请及时绑定邮箱和手机立即绑定

3 个 for 循环打印以下模式:

3 个 for 循环打印以下模式:

白板的微信 2023-10-12 17:35:27
我侄女在学校做作业时问我这个问题,我不知道该怎么做。老师要求他们在 java 中使用 3 个 for 循环打印以下模式:1******12*****123****1234***12345**123456*1234567请帮忙。谢谢!
查看完整描述

3 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

这曾经是我的家庭作业


代码


for (int i = 1; i <= 7; i++) {

    for (int j = 1; j <= i; ++j) {

        System.out.print(j);

    }

    System.out.println("");

}

将会呈现


1

12

123

1234

12345

123456

1234567


for (int i = 1; i <= 7; i++) {

    for (int k = 7 - i; k >= 1; k--) {

        System.out.print("*");

    }

    System.out.println("");

}

将会呈现


******

*****

****

***

**

*

最终的


for (int i = 1; i <= 7; i++) {

    for (int j = 1; j <= i; ++j) {

        System.out.print(j);

    }

    for (int k = 7 - i; k >= 1; k--) {

        System.out.print("*");

    }

    System.out.println("");

}

将会呈现


1******

12*****

123****

1234***

12345**

123456*

1234567


查看完整回答
反对 回复 2023-10-12
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

public static void printPattern(int n) {

        for(int i=0; i<n; i++) {

            for(int k=1; k<=i+1; k++) {

                System.out.print(k);

            }

            for(int j=i+1; j<n; j++) {

                System.out.print("*");

            }

            System.out.println("");

        }

    }


查看完整回答
反对 回复 2023-10-12
?
MYYA

TA贡献1868条经验 获得超4个赞

  Are you sure question has been asked to solve by using 3 for loops?

    As it is better to use less loop as much as we can. Secondly there is no requirement in problem to use third loop. you can find the desired result by using two loops:


    public class Main

    {

        public static void main(String[] args) {

               for (int i = 1; i <= 7; i++) {

                for (int j = 1; j <= 7; j++) {

                    if (j <= i) {

                            System.out.print(j);

                    }

                    else

                    {

                        System.out.print("*");

                    }

            }

                System.out.println("\n");

        }

        }

    } 


output will be:

1******

12*****

123****

1234***

12345**

123456*

1234567


查看完整回答
反对 回复 2023-10-12
  • 3 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信