Hi i am doing my coursework and i have been given the task is to make an algorithm for a square that’s 5×5 using “*” but has to be filled in with “.” so like this:
*****
*...*
*...*
*...*
*****
I have used this code I know its probably very messy because im an absolute beginner to this stuff. I cant seem to get the “.” in i currently have:
*****
*****
*****
*****
*****
here 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;
}
}
p.s sorry for been so bad at coding 😀 and any help would be much appreciated thanks Gareth
Have you gotten the program to print an 5×5 square full of asterisks working?
I’d strongly recommend getting that working first. After that, you just need a minor modification to print the dots in between.
For that, instead of always printing an asterisk, you’ll need a conditional.
Pseudocode:
I’ll leave it to you to figure out how to translate this into real code. Good luck!
Edit, hints on how to determine if you’re currently on the edge:
to print a square exactly 5×5 characters).
Hope that helps. There’s really not much more I can say without giving away the exact answer.
Finally, think about your solution before coding it. Immediately jumping into coding can be a roadblock, especially when you’re a beginner and not too familiar with the language so you get bogged down in the language syntax. Mentally put together the flow of your program, draw pictures, and maybe write pseudocode before you write the actual code. I find that doing this helps me write code easier and reduces the number of bugs that come up.