Solved

java triangle

Posted on 2011-03-01
5
728 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
ID: 35014431
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
ID: 35014444
0
 

Author Comment

by:pauledwardian
ID: 35014481
can you do these two triangles  by for loops please?

thanks,
edward
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35014812
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
ID: 35015854
		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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

930 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

9 Experts available now in Live!

Get 1:1 Help Now