Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

loops - Inverted Triangle

I have a project due where I have to draw an inverted triangle using the for loop, however I have to use the word that the person inputed to make the triangle.

So... the output should look like this...

Enter a word:  hello
hello
hell
hel
he
h

however... mine looks like this

Enter a word: hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello 

The code I printed looks like this:

System.out.println("Enter a word");
Scanner keyboard = new Scanner(System.in);

String a = keyboard.nextLine();
int rows = a.length()-1;

for(int i = rows; i > 0; i--){
  for(int j = i; j > 0; j--){
    System.out.println(a);
  }
}

I tried to search up how to fix it online, but the only thing that comes up is how to make triangles using * signs, but that doesn't seem to work when using words.

If anyone could help me figure out to get the above result it would be greatly appreciated.

question from:https://stackoverflow.com/questions/65947090/inverted-triangle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...