hey
i have a little problem, how do i make this two triangle:

O
O O
O O O
O O O O

O
O O
O O O
O O O O

i have to get input from user, that part i have done.
but the problem is loops and how to print the result out.
each letter i justified to the left with 4.

great thanks to anyone that could help me.

public class Oppgave1d {

	public static void main(String[]args) {
		Scanner input = new Scanner(System.in);

		System.out.print("Hvor mange linjer? ");
		int linje = input.nextInt();
		System.out.print("Hvilken bokstav? ");
		char bokstav = input.next().charAt(0);

		for (int i = 0; i < linje; i++) {
			for (int j = 0; j < linje; j++)
				System.out.printf("\n%-4S ", bokstav);
		}
	}
}

Edited 5 Years Ago by olsane: n/a

for(int i=0;i<4;i++){


for(int j=0;j<=i;j++){
System.out.print("*");
}
System.out.println();
}

@coding101 How about explaining to the OP the trick in the second for statement.

Spoon(Shovel) feeding a student with code he can copy and paste is not always the best way to get the student to think thru a problem and work out the solution.
You learn by trying and failing. Copy and pasting leaves very little knowledge gain.

one of the triangle got solved, but one remaining.
how can i used this code to get a triangle that look like a pyramid?

Take a piece of paper and draw the pyramid made of Xs.
Look at the first line: it has some spaces then the X.
Now look at the second line: it has one fewer space and 2 more Xs
And so on.
There is a pattern here. Try to work out how to write a loop that will set the number of spaces and the number of Xs for each line.

This article has been dead for over six months. Start a new discussion instead.