我正在尝试制作一个接受单词然后对角显示该单词的程序。到目前为止我只让它垂直显示。
扫描仪接受“zip”,然后输出:
z
i
p
我该如何让它像这样:
z
i
p
这是我的代码:
import java.util.Scanner;
public class exercise_4
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your words");
String word = scan.nextLine();
for (char ch: word.toCharArray())
{
System.out.println(ch);
}
}
}
最佳答案
你可以尝试这样的事情:-
String s = "ZIP";
String spaces = "";
for (int i = 0; i < s.length(); i++) {
System.out.println(spaces + s.charAt(i));
spaces += " ";
}
关于java - 如何在Java中对角显示单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159010/