Hello guys,

I'm really stumped on this assignment. What I need to do is make a triangle's height based on the number entered by the user. So if the user enteres 4, the output would be this:

$
$$
$$$
$$$$

And I can't figure out how to do that. What I have so far will print '$' down the screen. If I enter '4', it will print '$' down the screen 25 times, and of course, I know that's not right.

// Name: John Mollman
// Class: Java 1 - 5271
// Abstract: CHomework4A



// Imports -------------
import java.util.Scanner;

public class CHomework4A
{
	public static void main(String[] astrCommandLine)
	{
		// Get data from the user and store it in the variable 'intTriangleHeight'
		Scanner inputDevice = new Scanner(System.in);
		int intTriangleHeight = 0;
		
		
		// Loop until the user enters a number within 1 and 20
		do
		{
			System.out.println("Height of the triangle?");
			intTriangleHeight = inputDevice.nextInt();
		} while ((intTriangleHeight < 1) || (intTriangleHeight > 20));
		
		
		// Set a number of lines based on the height of the trianle
		// entered above
		for (int intIndex1 = 0; intIndex1 <= intTriangleHeight; intIndex1 += 1)
		{
			String strOutputLine = "$";		// Defines '$' as a building block
			
			// Print the number of '$' signs to go across
			for (int intIndex2 = 0; intIndex2 <= intTriangleHeight; intIndex2 += 1)
			{
				System.out.println(strOutputLine);
			}
		}
	}
}

You could use the StringBuilder class with the append() method, there are other methods in that class that would achieve the adding $'s to a new line using a for loop as your iterator. Take a look at these docs for all your string manipulation needs.

The other way (just glancing at the code) might be to use only one for loop that takes 2 initializers and 2 iterators, I haven't tried it yet.

Is this the algorithm for producing the output:
Print on each row then the number of $ equal to the row number. Ie 1 on first, 2 on second, etc

That implies nested loops, one for the row and one for the row's contents.

The first for loop will execute the nested for loop as many times as intTriangleHeight is initialized to be in addition to the iterator on the first for loop that compounds it.

also your println(strOutputLine) will never print any columns, just rows since it basically adds a "\n", you might want System.out.print(strOutputLine) with a "println() some where seperate in your loop.

Step through your code with a debugger, you'll be able to see just how many times your inner and outer for loop are reiterating.

jwmollman,
Can you modify your code by the following 2 changes?
(1) in line 34, replace intTriangleHeight with intIndex1 so that the line of code will be:

for (int intIndex2 = 0; intIndex2 <= intIndex1 ; intIndex2 += 1) // control the number of symbles for each row

(2) replace the code from line 35 and 37 with

System.out.print("S"); // print certain number symbles for each row
System.out.println();  // go to new line after completing printing for each row

(3) delete line 31

You may have to make more changes to accomplish the task (to adjust the number of rows) although the triangle is shown.
Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.