Results 1 to 2 of 2
Thread: making triangle using *
- 09-13-2011, 08:37 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
making triangle using *
I have searched for this and found many similiar questions. The trick here is to not make the exact same image as below. The triangles need to be seperate.
Write a java application that displays the following patterns separately one below the other.
Use for loops to generate the patterns. There are 4 Triangle patterns.
All asterisks (*) should be printed by a single statement of the form System.out.print('*');
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********
I have figured out the 1st triangle and the third triangle. Although I don't completely understand why the third triangle works. I can't seem to figure out the 2nd and 4th. Here is what I have:
//This program display 4 triangle patterns using for loops
//Use the Scanner class to create an object to read input from system.in
public class Triangle3 {
public static void main (String[] args){
/*Create for loop for triangle 1
/* loop 10 times
*/
int row;
// maximum number of rows
int maxRows = 10;
//create a loop
for (row = 1; row <= maxRows; row++) {
//print *
int firstTri;
for (firstTri = 1; firstTri <= row; firstTri++){
System.out.print ("*");
}
//print a new line
System.out.println();
}
row = 1;
maxRows = 10;
//Create for loop for triangle 3
for (row = 1; row <= maxRows; row++) {
//print *
int thirdTri;
for (thirdTri = 10; thirdTri >= row; thirdTri--){
System.out.print ("*");
}
//print a new line
System.out.println();
}
}
}
- 09-13-2011, 09:05 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Re: making triangle using *
You should decompose your problem(s) in manageable chunks; have a look at this small method:
You have to pass it a char and an int n; it'll print n of those chars on one line; it can be used by the following two methods:Java Code:public static void printNChars(char c, int n) { for (int i= 0; i < n; i++) System.out.print(c); }
The first method prints n stars while the second method prints n spaces (whatever the value of n may be). Now try to formulate your original problem(s) in terms of the two little methods above.Java Code:public static void printNStars(int n) { printNChars('*', n); } public static void printNSpaces(int n) { printNChars(' ', n); }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
triangle
By Shyamz1 in forum New To JavaReplies: 4Last Post: 11-07-2010, 06:12 PM -
Outputting a * triangle
By blackbeltsas in forum New To JavaReplies: 1Last Post: 10-17-2010, 11:39 AM -
Triangle using Numbers
By Anandt88 in forum New To JavaReplies: 16Last Post: 06-05-2010, 04:10 PM -
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01-10-2009, 02:08 PM -
Making triangle
By banie in forum New To JavaReplies: 4Last Post: 02-02-2008, 11:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks