0

私が現在行っている作業では、「*」をアウトラインとして印刷し、「.」を印刷して正方形を「描く」必要があります。コードは機能しているようですが、まだ受け入れられません。私は初心者です、ありがとう。

class Main {

    public static void main(String args[]) {

        int square = 5;
        int line = 1;
        int stars = 1;
        int startLine = square / square;

        while (line <= startLine) {

            while (stars <= square) { // first line prints out dots for the value of square, in this case 5
                System.out.print("*");
                stars = stars + 1;
            }

            System.out.println();
            line = line + 1;                //prints new line and adds 1 to line. Meaning it will move to the next section as it is now greater than startLine
        }

        stars = 1;              //resets stars

        while (line <= square - 1) {// line will keep looping until its value is greater than square - 1

            while (stars <= startLine) {
                System.out.print("*");         // First character to print is *
                stars = stars + 1;
            }

            while (stars <= square - 1) {
                System.out.print(".");          //prints 3 dots
                stars = stars + 1;
            }

            while (stars <= square) {
                System.out.print("*");         // stars is equal to square so 1 star is printed 
                stars = stars + 1;
            }

            stars = 1;
            System.out.println();
            line = line + 1;              // adds a value and loops around again
        }

        stars = 1;
        while (line <= square) {

            while (stars <= square) {
                System.out.print("*");
                stars = stars + 1;
            }

            System.out.println();
            line = line + 1;
        }
    }
}

正方形を構成しているようで、「正方形」の値を変更するとサイズも変更されます。問題がわかりません。

4

1 に答える 1

0

これを行うには、行ごとにループするより良い方法があります。あなたが考えることができることの1つは、それが正方形であることを知っているので、あなたの場合は常に5x5、または6x6、7x7などのようなものです...

正方形の幅を x、高さを y としましょう。インデックスが*x55x*1x*その他*x1が " ."の場合にのみ星が必要です。

だからここにいくつかの擬似コードがあります

get width or height from user then set either the height or the width to the same thing that way its a square

for loop 1 to 5 //x - width
    for loop 1 to 5 //y - height
        if (check for *x5, 5x*, 1x* and *x1) //you can make a couple IF's if you want
            print a star
        else 
            print a dot
        end if
     print new line ("\n")
    next
next

これが役立つことを願っています。

于 2012-07-19T13:20:29.737 に答える