Solved

java triangle

Posted on 2011-03-01
5
718 Views
Last Modified: 2012-05-11
how is it possible to draw this type of triangle in java:

    *
  **
***
and

***
 **
   *
0
Comment
Question by:pauledwardian
5 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
What do you mean by that question?
You cat just print like that:

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

or you can create a panle and draw them and fill them in.

Please, explain what do you mean by this question?

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 

Author Comment

by:pauledwardian
Comment Utility
can you do these two triangles  by for loops please?

thanks,
edward
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
Comment Utility
put for loop !
and use system.out.print

for each(increment) it will print one *

put if  inside the loop
system.out.println

0
 
LVL 3

Accepted Solution

by:
rmonsen earned 500 total points
Comment Utility
		int trisize = 3;

		// Draw first triangle
		for (int i = 1; i <= trisize; i++) {
			// Spaces infront
			for(int j = 0; j < trisize - i; j++ ) {
				System.out.print(" ");
			}
			// stars
			for(int j = trisize; j > trisize - i; j-- ) {
				System.out.print("*");
			}
			System.out.println();
		}

		// Draw second triangle
		for (int i = trisize; i >= 1; i--) {
			// Spaces infront
			for(int j = 0; j < trisize - i; j++ ) {
				System.out.print(" ");
			}
			// stars
			for(int j = trisize; j > trisize - i; j-- ) {
				System.out.print("*");
			}
			System.out.println();
		}


		// Draw both in one loop
		for (int i = trisize; i >= -trisize; i--) {
			int stars = 1 + trisize - Math.abs(i);
			if (i == 0) continue; // Skip iteration
			
			for(int j = 0; j < trisize - stars; j++ ) {
				System.out.print(" ");
			}
			// stars
			for(int j = trisize; j > trisize - stars; j-- ) {
				System.out.print("*");
			}
			System.out.println();
		}

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now