0
public class test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Please insert a word.:  ");
        String word = (" ");
        while (in.hasNextLine()){
            System.out.println(in.next().charAt(0));
        }
    }
}

入力から各文字を読み取り、スペースで区切ろうとしています。

例: 入力はYes.

出力は

Y
E
S
.

char を入力の次の文字に移動する方法がわかりません。誰でも助けることができますか?

4

2 に答える 2

1

ループの「hasNextLine」にバグがあります-余分な ; ループ本体の前にセミコロン。セミコロン (何もしない) がループされ、本体が 1 回実行されます。

それを修正したら、単語内の文字をループする必要があります。「hasNextLine」ループ内:

String word = in.nextLine();
for (int i = 0; i < word.length(); i++) {
    char ch = word.charAt(i);
    // print the character here..  followed by a newline.
}
于 2013-10-08T01:39:04.233 に答える
1

あなたができる

while (in.hasNext()) {
    String word = in.next();
    for (char c: word.toCharArray()) {
        System.out.println(c);
    }
}
于 2013-10-08T01:41:31.003 に答える