使用while循环显示用户按垂直顺序输入的字母?

时间:2016-09-06 02:08:11

标签: java words

我正在尝试创建一个脚本,要求用户输入一个单词,然后以垂直顺序显示用户输入的字母。问题是我需要使用while循环,任何想法??????

import java.util.Scanner;
public class VerticalWords {

    public static void main(String[] args) {


               System.out.println("Enter A word");
                Scanner scan = new Scanner(System.in);


                String word = scan.nextLine();

                for(char a : word.toCharArray())
                {
                   System.out.println("Letter: " + a); 
            }
    }
}

我已尝试过该代码,但它可以工作,但它不是一个while循环^

2 个答案:

答案 0 :(得分:1)

试试这个:

System.out.println("Enter A word");
Scanner scan = new Scanner(System.in);
int a = 0;

String word = scan.nextLine();

while(a < word.length){
    System.out.println(word.charAt(a));
    a++;
}

答案 1 :(得分:0)

import java.util.Scanner;

公共类VerticalWords {

@Override
public void onResume() {
    // Your initialization
}

}