Results 1 to 7 of 7
Thread: Box of asterisks
- 11-01-2011, 11:02 PM #1
Member
- Join Date
- Nov 2011
- Location
- Canada
- Posts
- 2
- Rep Power
- 0
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
- 11-01-2011, 11:19 PM #2
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.
- 11-01-2011, 11:30 PM #3
Member
- Join Date
- Nov 2011
- Location
- Canada
- Posts
- 2
- Rep Power
- 0
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.
- 11-01-2011, 11:39 PM #4
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.
Last edited by Junky; 11-01-2011 at 11:50 PM.
- 11-01-2011, 11:49 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
Re: Box of asterisks
Ignore this
Last edited by skaterboy987; 11-01-2011 at 11:53 PM.
- 11-01-2011, 11:52 PM #6
Re: Box of asterisks
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.
- 11-01-2011, 11:53 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
Priting numbers as asterisks & spaces
By Cnos in forum New To JavaReplies: 20Last Post: 10-25-2011, 06:49 AM -
Turning numbers into asterisks.
By BugginVT in forum New To JavaReplies: 7Last Post: 02-25-2009, 08:38 AM -
printing an "E" out of asterisks via strings
By hokieman07 in forum New To JavaReplies: 1Last Post: 04-08-2008, 06:45 AM -
asterisks triangles
By Dan121 in forum New To JavaReplies: 1Last Post: 01-12-2008, 08:42 PM
Bookmarks