亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號