Solved

java triangle

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

    *
  **
***
and

***
 **
   *
0
Comment
Question by:pauledwardian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

690 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