Results 1 to 11 of 11
  1. #1
    andypatel00 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Smile How to mirror Star Pattern

    Hello everyone,

    I am fairly new to Java, and am stuck on a basic program.... we have to write four star pattern programs I got the first two, but can't seem to mirror the image to print on "right alignment"

    I have:

    Java Code:
    *
    **
    ***
    ****
    *****
    Need to mirror to print:

    Java Code:
        *
       **
      *** 
     ****
    *****
    Thank you

    Java Code:
    for (row=number; row>=0; row--) {
    for (col=1; col<=row; col++) {
    System.out.print("*" + "");
    Last edited by Fubarable; 04-03-2011 at 02:45 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    andypatel00 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    When I tried to add another loop for the spaces..... it gives me like a variable error... can I just make up a variable???

    Java Code:
    public static void main(String arg[])
    	{
    	//declare variables
    	int number;
    	int row;
    	int col;
    	System.out.print("Please enter a number:");
    	Scanner myScanner  = new Scanner(System.in);
    	number = myScanner.nextInt();
    	//start loop
    
    	for (row=number; row>=0; row--) {
    	       for (col=1; col<=row; col++) {
    			System.out.print("*");
    	       }
    	       System.out.println();
    
    for (row=1; row<=number; row++) {
    	       for (col=1; col<=row; col++) {
    			System.out.print(" ");
    	       }
    	       System.out.println();
    Last edited by Fubarable; 04-03-2011 at 07:35 PM. Reason: code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    think of how you would do this on paper. Would you print your spaces after printing the stars on a line (as you're trying to do now), or before the stars? How many spaces should there be on each line (as a function of the line number)?

    Also, you'll want to use well-formated code with standard indentation so that you can see your errors easier, and so that we'll be able to understand your code better. Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    I've added code tags to your post above, but the formatting was broken to begin with and you'll likely want to fix that, again so that you and we can see the errors more readily.

  5. #5
    andypatel00 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    //Code

    // thanks for all of your help, I am a beginner... and trying to mirror a star pattern.... I got the first line to shift over, but can't figure out how to loop for the rest of the stars

    System.out.print("Please enter a number:");
    Scanner myScanner = new Scanner(System.in);
    number = myScanner.nextInt();
    //start loop

    for (row=number; row>=0; row--) {
    for (col=1; col<=row; col++) {
    System.out.print(" ");
    }
    //System.out.println();

    for (row=1; row<=number; row++) {
    for (col=1; col<=row; col++) {
    System.out.print("*");
    }
    System.out.println();
    }
    //end of code

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

    Default

    The "Printing Star Patterns" as homework is often seen on this and other fora. The best way to solve this is to write another method which prints out N number of characters. then your controlling method simply determines what character to print, how many times and when. Then calls the other method.
    Java Code:
    loop number of rows {
        call print X spaces
        call print Y chars
        change values
    }

  7. #7
    andypatel00 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    I am understanding what you are saying, but still having trouble... keeps on going (never ending loop) how or what line do I look at to stop it once it hits the number inputed...

    Thanks again....

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

    Default

    We don't read minds. Post your code.

  9. #9
    andypatel00 is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    I hope this helps.... not knowing how to post code

    sorry



    //Code

    // thanks for all of your help, I am a beginner... and trying to mirror a star pattern.... I got the first line to shift over, but can't figure out how to loop for the rest of the stars

    System.out.print("Please enter a number:");
    Scanner myScanner = new Scanner(System.in);
    number = myScanner.nextInt();
    //start loop

    for (row=number; row>=0; row--) {
    for (col=1; col<=row; col++) {
    System.out.print(" ");
    }
    //System.out.println();

    for (row=1; row<=number; row++) {
    for (col=1; col<=row; col++) {
    System.out.print("*");
    }
    System.out.println();
    }
    //end of code


    thanks again

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

    Default

    If you want to do it your way and ignore my advice then take a close look at what you code is doing. You have a nested loop that prints all the spaces and ends. You then have another nested loop that prints all the stars. My pseudocode above gives you an idea of how this should be done.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    See if this helps, this problem can be done with one nested loop and an if else clause. Take some time and really think about it. Also, read fubarables post, he explains how to wrap your code in code tags and gives some good advice in general.

Similar Threads

  1. Star pattern help?
    By GeeKunMow in forum New To Java
    Replies: 13
    Last Post: 07-21-2011, 05:59 AM
  2. Star triangle pattern
    By crazy4fun in forum New To Java
    Replies: 7
    Last Post: 03-06-2011, 01:03 PM
  3. Need help with mirror images for dr.java
    By outlawstar7788 in forum New To Java
    Replies: 1
    Last Post: 04-07-2010, 10:44 PM
  4. intricate star pattern
    By aamster in forum New To Java
    Replies: 7
    Last Post: 10-11-2009, 06:50 AM
  5. Recursive Star Pattern HELP PLEASE!!
    By ITnewbie in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 09:55 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
  •