-
Box of asterisks
Hello
First year Computer Science student here. I'm completely stuck as how to how get an open box of asterisks as my output.
For example entering a length of 4 and a width of 4 to produce
****
* *
* *
****
any help would be appreciated!! so far I have:
import java.util.Scanner;
public class Stars {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
int length; // number of stars on a side
int width; // the current column of the square being printed
int row = 0;
// prompt user to enter the length and width
System.out.print("Enter the length: ");
length = input.nextInt();
System.out.print("Enter the width: ");
width = input.nextInt();
System.out.print("*");
System.out.println();
} // end of main
} // end of Stars
-
Re: Box of asterisks
First and last row is simple: print N stars.
For every other row: print star, print M spaces, print star, newline.
Try drawing what the output would look like for various values: 4,5,6,7 etc on a piece of (graph) paper and you should be able to see a pattern. Once you can determine what that pattern is then writing the code is not that hard.
-
Re: Box of asterisks
I see where your coming from. I really just don't know how to go about writing the code for that specific part.
-
Re: Box of asterisks
Have you worked out the pattern/algorithm? If so post it here so we can see. As I said working out the algorithm is the hard part, wiriting the code is the easy part. Why is it easy? Because you know how to declare variables, how to change the value of variables, write loops, write if statements, use print statements. The program is just a bunch of all those things that you know how to do. You just have to get them in the correct order and that is where the algorithm comes in.
-
Re: Box of asterisks
-
Re: Box of asterisks
Quote:
Originally Posted by
skaterboy987
I did it.
No you didn't. The requirement is for the box to be empty, spaces inside. Even if you did solve please DO NOT post the code. We don't care how clever you are. All you do is enable someone to cheat and hinder their learning if they just copy your code. Ooops! It was only pseudocode which is ok.
-
Re: Box of asterisks