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


}

Recommended Answers

All 20 Replies

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

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

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();
}

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

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.

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
could u post the code for a diamond please.
i have problem with the program.
thanks.

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

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

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.

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

commented: very rude -1

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

Okay cool

So 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

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

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

Hmm you make a valid point
*facepalms*

Why the heck did TheBuzzer have to bump this XD

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

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.

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.

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

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

Start a new thread. And use 2 for loops one inside the other.
The 1st iteration of the loop will print 1 only 1 time.
The 2nd iteration of the loop will print 2 only 2 times.
The 3rd iteration of the loop will print 3 only 3 times.

The 1,2,3 are the index of one of the loops

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.