有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在单词中的每个字符上返回一个字符?

例如,如果您键入happy,它将显示

h

a

p

p

y

现在我有这个:

    public static void three(){

    String x = keyb.next();

         int  y= x.length();

    char []  word = new char [y];

        System.out.println("It has " + y);
    for (int i = 0; i < x.length(); i++) {
        System.out.println(x);
    }

}

这只会让这个词看起来有多少个字符。但我只想让每个字符本身缩进。请,谢谢


共 (1) 个答案

  1. # 1 楼答案

    就这么简单:

    String str = "happy";
    for (int i = 0 ; i < str.length() ; i++){
        System.out.println(str.charAt(i));
    }