Output a block to the console in Java

Hi, I'm doing my course. My task is to use "*" to make a 5 × 5, but you must use "." Fill in like this:

*****
*...*
*...*
*...*
*****

I've used this code and I know it can be very confusing because I'm an absolute beginner I can't seem to get "." At present, I have:

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

This is my code:

public static void main( String args[] )
{
    System.out.print ("#size of square");
    int stars=BIO.getInt();
    int j=1;
    while(j <= stars)
    {
        int starsNumber=1;
        while (starsNumber<= stars)
        {
            int i = 1;                        // Display trunk
            starsNumber=starsNumber+1;
            System.out.print('*');
        }
        System.out.println();

        j= j +1;
    }
}

Sorry, the coding is terrible: D and any help would be very grateful to Gareth

Solution

Did you print out a 5 × 5 square full asterisk working procedure?

I strongly recommend that it work first After that, you can print dots between the two with only a few minor modifications

To do this, you need a condition instead of always printing an asterisk

Pseudo code:

if(I'm currently on the edge)
  Print an asterisk
else
  Print a dot

I'll leave it to you to figure out how to translate it into real code Good luck!

Edit and prompt how to determine whether you are currently in an advantageous position:

>You are tracking the current row and the current column (printing an exact 5 in order) × 5-character Square). > The edge consists of the first and last rows and the first and last columns. > Given prompts 1 and 2, you can now determine when to be on the edge and when not to process? You need to compare variables. If you are not familiar with how to compare variables, please read your textbook

I hope it will help If I don't reveal the exact answer, I can really say more

Finally, consider your solution before coding Jumping into coding immediately can be an obstacle, especially if you are a beginner and are not familiar with the language Before writing the actual code, carefully organize the program flow, draw pictures, and write pseudo code I find this helps me write code more easily and reduce the number of errors

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>