第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

我怎樣才能在java中修復(fù)這個星星金字塔

我怎樣才能在java中修復(fù)這個星星金字塔

慕少森 2022-10-26 17:21:46
我必須通過用戶輸入打印星星的金字塔,用戶輸入:多少行,多少列。我盯著一顆星,每次迭代將星數(shù)增加 2,將行數(shù)減少 1。我無法確定我必須做多少空間。我必須做什么:示例:printStars(4,2) rows = 4 , columns = 2.output :   *       *  ***     *** *****   ************ *******printStars(3,3) rows= 3 , columns =3.output :   *     *     * ***   ***   ******** ***** *****printStars(3,4) rows = 3 , columns =4.output:  *     *     *     * ***   ***   ***   ******** ***** ***** *****編碼:private static void printStars(int rows, int columns ) {        int stars = 1;        while (rows > 0) {            int spaces = rows;            for (int i = 1; i <= columns; i++) {                for (int sp = spaces; sp >=1; sp--) {                    System.out.print(" ");                }                for (int st = stars; st >= 1; st--) {                    System.out.print("*");                }            }            System.out.println();            stars += 2;            rows--;        }    }我得到了什么:printStars(3,4)output:   *   *   *   *  ***  ***  ***  *** ***** ***** ***** *****
查看完整描述

1 回答

?
喵喵時光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個贊

乍一看,您似乎沒有考慮打印星星后出現(xiàn)的空間。嘗試像這樣修改代碼:


private static void printStars(int rows, int columns)

{


    int stars = 1;


    while (rows > 0) {


        int spaces = rows;


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


            for (int sp = spaces; sp >= 1; sp--) {

                System.out.print(" ");

            }

            for (int st = stars; st >= 1; st--) {

                System.out.print("*");

            }

            for (int sp = spaces; sp >= 1; sp--) {

                System.out.print(" ");

            }

        }

        System.out.println();

        stars += 2;

        rows--;

    }

}


查看完整回答
反對 回復(fù) 2022-10-26
  • 1 回答
  • 0 關(guān)注
  • 96 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號