Java 如何使用循环而不是数组提取单词

Java 如何使用循环而不是数组提取单词,java,for-loop,Java,For Loop,情况: 用户将输入一个句子。 我需要对每个单词进行某些修改,但这取决于每个单词的长度 例如,如果单词长度超过2,我需要在每个元音前加上3 // for words with length > 2 for (i=0;i<example.length();i++) switch (word.charAt(i)) { case 'a': case 'e': case 'i': case 'o': case 'u': case

情况:

用户将输入一个句子。 我需要对每个单词进行某些修改,但这取决于每个单词的长度

例如,如果单词长度超过2,我需要在每个元音前加上3

// for words with length > 2
for (i=0;i<example.length();i++)

    switch (word.charAt(i))
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    case 'y':
        output += "3" + word.charAt(i);
        break;
    default:
        output += word.charAt(i);
        break;
    }
//对于长度大于2的单词
对于(i=0;i
公共类测试{
公共静态void main(字符串[]args){
String input=“你好,我叫罗杰”;
input+='';//在末尾添加一个空格以指示完成最后一个单词
字串=”;
char ch;
字符串res=“”;
int len=input.length();

对于(int i=0;i)到目前为止,您尝试过什么?您尝试过递归吗?
public class Test {
  public static void main(String[] args) {
     String input = "hello my name is roger";
     input+=' '; // adding a whitespace at end to indicate completion of last word

     String word = "";
     char ch;
     String res = "";
     int len = input.length();

     for(int i = 0;i<len ;i++) {
       ch = input.charAt(i);
       if(Character.isWhitespace(ch)) {
         res = res +" "+processWord(word);
         System.out.println(word);
         word = "";
       }else {
         word+=ch;
       }
     }
}

  private static String processWord(String word) {
    // TODO Auto-generated method stub
    if(word.length()<=2) {
      return word;
    }

    // do whatever you have to do with your word
    String res = "";
    return res;
  }
}