If this is a homework or a lab exercise for your computer science subject read on and get the solution!
"For" loops are efficient if you know how many times you want to repeat or execute the statement or group of statements, so here the counters (i and j) will start at 1 and end with the desired width of the inverted triangle. Remember, that if your counters start at 1 (i.e. i=1) it will terminate at a value less than or equal to that value (i.e. i<=width) to avoid "Off by One" errors.
//the class starts here!
public class InvertedTriangle{
private int width;
//we will use a constructor to set the value of "width"
public InvertedTriangle (int _width){
width = _width;
}
//the method to create the inverted triangle with "width"
public String toString(){
String r = "";
int num = width;
for(int i=1; i<=width; i++){
for(int j=1; j<=num; j++){
r = r + "*";
}
num = num - 1;
r = r + "\n";
}
return r;
}
}
so that's that.. :D So we make another class to test "Inverted Triangle". We will make another class "TestInvertedTriangle" with the main method (using 10 as width, you can change this value if you want..)
public class TestInvertedTriangle{
public static void main(String[] args){
InvertedTriangle c = new InvertedTriangle(10);
System.out.print(c.toString());
}
}
if you have any questions, please do leave a message!
NOTE: This is a program that I made myself. I have tested it and came out with the desired output. I am also new in Java programming and I may not have applied the "elegant way" of writing Java programs. So please do understand. :D
Hai ddenz,
I came across ur article in net,it was useful to me.But i am beginner in programming,i am really afraid of the for loops especially one that prints different shapes like triangle ,paralellogram etc.,i have lot of school assignments of these kind.I want to know how to print different structures using for loops..can u please make me understand..by sending some articles to my mail ..i feel really inferior when my class mates finish them so quickly than me.. Expecting ur reply
This is nothing because, education is implemented in schools also demand to maximize the skills...
Do you have children before the wants of his reputation as a class, the teacher know or do they...
“Find me a tutor” is a commonly heard phrase. Nevertheless, people who know what kind of pe...
Now that everyone is asking others about ‘find me a tutor’ as looking for a private tutor f...
Basic rules in child-rearingChild's behavior, whether accepted or rejected enhanced rewards he ...
Are you ready for summer? Play up your swim wear with colorful accessories this summer. Make yo...
Craving for the perfect Charm Bracelet but can't find it in the stores or online? We have the s...
So you've downloaded the .abr file, what's next? Read this tutorial and learn the how-to!...
Ever wondered how those pros did it? Learn it here! Turn your photos into amazing vectors as a ...
Asia has different recipes for fried plantains, and I'm sharing mine! Real easy and delicious!...