954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Troubles with printing triangles

Alright, Ive moved onto my next mission. I have to draw a set of four triangles using for loops. I have the first two done, the second two have to have spaces prior to printing the asterisks. example:
*****
****
***
**
*

*
**
***
****
*****


This is what I have so far, which does print the first two fine, I just needto know how to get my spacing correct. Any suggestions or tips are welcome, thanks in advance, NewbyChic

public class Triangle

{

// Displays a triangle shape using asterisks

public static void main ( String[] args)

{

int maxNumRows = 10;//maximum number of rows

for ( int row = 1; row <= maxNumRows; row++ )//inner loop

{

for ( int firstTri = 1; firstTri <= row; firstTri++ ) // outer loop

System.out.print("*");

System.out.println();

}

System.out.println();

maxNumRows = 10; //minimum number of rows
for ( int row = 1; row <= maxNumRows; row++ ) //inner loop

{
for ( int secondTri = 10; secondTri >= row; secondTri-- ) //outer loop

System.out.print("*");
System.out.println();
}


} // end of main method

}

NewbyChic
Newbie Poster
7 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

good old homeworks... :) here is a tip for you.... you need to use variables from the outer loops in the inner loops... 1. row 1 star... 2. row 2 stars...
for the other one...
1. row n stars... 2. row n-1 stars...

hope that will make a sense :) good luck

ps. don't forget to put code tags, so we can read easily...

tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 

oops, i now see that you already did what i say... sorry :)
you put

System.out.println();

this line is in wrong place(inner loop)... so consider putting them somewhere else....

tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 

nevermind, i figured it out:

System.out.println();

maxNumRows = 10; //maximum number of rows
for ( int row = 1; row <= maxNumRows; row++ ) //inner loop
{
for ( int thirdTri = 10; thirdTri >= row; thirdTri-- ) //print asterisks
System.out.print("*");

System.out.println();

for ( int thirdTri = 1; thirdTri <= row; thirdTri++ ) //print spaces
System.out.print(" ");
}

System.out.println();

maxNumRows = 10; //maximum number of rows
for ( int row = 1; row <= maxNumRows; row++ ) //inner loop
{
for ( int fourthTri = 9; fourthTri >= row; fourthTri-- ) //print spaces
System.out.print(" ");

for ( int fourthTri = 1; fourthTri <= row; fourthTri++ ) //print asterisks
System.out.print("*");

System.out.println();
}

NewbyChic
Newbie Poster
7 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

i totally misunderstood your question... :D
my mind is still in sleep....

tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 

I know what ya mean, I program myself to sleep. I am trying to teach myself this stuff through an online class. Not the easiest stuff to try and teach your own brain.

NewbyChic
Newbie Poster
7 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

I had to do a diamond once, making two triangles. I could post the code if you need it, but it looks like you've already got it solved.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

@ server_crash
could u post the code for a diamond please.
i have problem with the program.
thanks.

sky100
Newbie Poster
2 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

i want to print
******
****
**
*

marwa munir
Newbie Poster
1 post since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

So you want to print that. What are we supposed to do, write it for you?

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

Inside a for-loop you will have a System.out.print("*") How many times will this for loop will execute will determine how many * you will have at each line.
Then you will put it in another for-loop which you will use to determine how many lines you will have:

for () {
  for () {
     System.out.print("*") 
  }
  System.out.println(""); //for changing lines
}

What will you put in the for as arguments(counters) will be up to you.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

if u need help in this. you should quit. can't even do this simple homework shows that your skills should be somewhere else.

TheBuzzer
Junior Poster in Training
52 posts since Jul 2008
Reputation Points: 15
Solved Threads: 0
 
i want to print ****** **** ** *

Okay coolSo you want to print that. What are we supposed to do, write it for you?

Common, it isn't that hard

System.out.println(
******
****
**
*);

Have a nice day now :D

bloody_ninja
Junior Poster in Training
96 posts since Jul 2008
Reputation Points: 9
Solved Threads: 2
 

Close one, but you would have to add some new line characters to make that work =P

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

Quotes are always nice too. But I think bloody_ninja had his tongue firmly in cheek with that post. Hence the :D avatar. Also let me be the first to point out that this thread started in 2005, then was revived in February, then again in April, then again today (I'm on the West Coast, so it's still July 14).

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Hmm you make a valid point
*facepalms*

Why the heck did TheBuzzer have to bump this XD

bloody_ninja
Junior Poster in Training
96 posts since Jul 2008
Reputation Points: 9
Solved Threads: 2
 

Common, it isn't that hard

System.out.println( ****** **** ** *);


It don't care how easy it is. It is a matter of not doing anyone's work, even the simplest request. If you do that for them then they want you to do everything. http://www.daniweb.com/forums/announcement9-2.html

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

Hmm you make a valid point *facepalms*

Why the heck did TheBuzzer have to bump this XD

I was searching for using graphics 2d to do Strokes and Shapes and this post came up as one of the result.

And When i read it. I knew right away it was a homework problem and didn't notice a date.

I wonder do teachers still assign this as a homework problem. lets see I did this in like my first year in college which was 4 years ago.

TheBuzzer
Junior Poster in Training
52 posts since Jul 2008
Reputation Points: 15
Solved Threads: 0
 
It don't care how easy it is. It is a matter of not doing anyone's work, even the simplest request. If you do that for them then they want you to do everything. http://www.daniweb.com/forums/announcement9-2.html

Yes, I read that thread and I thought it was pretty apparent what he said, almost as if it was a joke.

I mean, he said "print" a few asterisks and there is a feature known as system.out.println.
Hey, it doesn't matter if they want you to do everything, it is up to you. But I just stated out a common function, not much harm done.

bloody_ninja
Junior Poster in Training
96 posts since Jul 2008
Reputation Points: 9
Solved Threads: 2
 

Hi... please help me to solve this....
I need to get the following output:

1
2 2
3 3 3
4 4 4 4

and so on...

arun123prasath
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You