Results 1 to 7 of 7
  1. #1
    golf_pro90 is offline Member
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default 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.

  3. #3
    golf_pro90 is offline Member
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default 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 10:50 PM.

  5. #5
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Box of asterisks

    Ignore this
    Last edited by skaterboy987; 11-01-2011 at 10:53 PM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Box of asterisks

    Quote Originally Posted by skaterboy987 View Post
    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.

  7. #7
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

Similar Threads

  1. Priting numbers as asterisks & spaces
    By Cnos in forum New To Java
    Replies: 20
    Last Post: 10-25-2011, 05:49 AM
  2. Turning numbers into asterisks.
    By BugginVT in forum New To Java
    Replies: 7
    Last Post: 02-25-2009, 07:38 AM
  3. printing an "E" out of asterisks via strings
    By hokieman07 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 05:45 AM
  4. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •