Results 1 to 11 of 11
Thread: How to mirror Star Pattern
- 04-03-2011, 02:38 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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:
Need to mirror to print:Java Code:* ** *** **** *****
Thank youJava Code:* ** *** **** *****
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
-
Have you tried printing spaces before the stars? Simple algebra should allow you to solve this.
- 04-03-2011, 07:31 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
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.Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 04-03-2011, 08:10 PM #5
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
//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
- 04-04-2011, 12:06 AM #6
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 }
- 04-04-2011, 02:51 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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....
- 04-04-2011, 03:20 AM #8
We don't read minds. Post your code.
- 04-04-2011, 03:26 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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
- 04-04-2011, 03:35 AM #10
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.
- 04-04-2011, 03:50 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Star pattern help?
By GeeKunMow in forum New To JavaReplies: 13Last Post: 07-21-2011, 05:59 AM -
Star triangle pattern
By crazy4fun in forum New To JavaReplies: 7Last Post: 03-06-2011, 01:03 PM -
Need help with mirror images for dr.java
By outlawstar7788 in forum New To JavaReplies: 1Last Post: 04-07-2010, 10:44 PM -
intricate star pattern
By aamster in forum New To JavaReplies: 7Last Post: 10-11-2009, 06:50 AM -
Recursive Star Pattern HELP PLEASE!!
By ITnewbie in forum New To JavaReplies: 1Last Post: 09-25-2009, 09:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks