I don’t understand why this doesn’t run. I am trying to make a program that displays the text the user inputs as a triangle. Each new line adds a letter which forms the triangle pattern.
For example, if the user enters the word computers, the output would be:
s
sr
sre
sret
sretu
sretup
sretupm
sretupmo
sretupmoc
import java.util.Scanner;
public class BackwordsTri
{
public static void main(String[] arg)
{
Scanner keyboard = new Scanner(System.in);
String word = keyboard.nextLine();
String newWord="";
char ch;
int wl = word.length();
for (int i=0; i < word.length(); i++)
{
int q = (wl - i);
ch= word.charAt(q);
newWord= ch + newWord;
System.out.println(newWord);
}
}
}
We might need a little more information to help you out on this one. It sounds like you want to be able to print the reversed version of the user input, incrementing by 1 additional letter per line starting from the last letter of the word.
Would you be able to add in the error that you are getting or what the output is from your program? along with a formatted version of the expected output if the issue is in fact formatting?