java没有得到输出

yk9xbfzb  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(406)

下面是我将句子中的所有单词转换成piglatin的代码,即“她的食物被偷了”转换成“erhay oodfay isay olenstay”,但是我得到的输出是erhay。如有任何更正,将不胜感激。谢谢。

public class piglatin
{
    public void main(String s)
    {
        s=s.toUpperCase();
        s=s+" ";

        int l=s.length(); 
        String word="";

        int n=0; 
        int w=0;//no of words in s(loop1)
        int wor=0;//no of words loop2
        for(int i=0;i<l;i++)
        {char c=s.charAt(i);
            if(c==' ')
            w++;

        }

        for(int i=0;i<l;i++)
       {  char c=s.charAt(i);
          int m=s.indexOf(' '); //length of first word

           if(i==0)
         { for(int j=0;j<m;j++)
             {char c1=s.charAt(j);             
              if(c1=='A'||c1=='E'||c1=='I'||c1=='O'||c1=='U')
              {n=j;//index of first vowel
              j=m;}
             }
             word=s.substring(n,m)+s.substring(0,n);
             System.out.print(word+"AY"+" ");

         }
         if(c==' '&&wor!=w-1)
         { s=s.substring(m+1,l);
             l=s.length();
             i=0;
             wor++;
         }
            if(wor==w-1)
            i=l+1;
        }

    }
}
p8ekf7hl

p8ekf7hl1#

通过在空格上拆分句子并处理结果数组中的每个单词,可以大大简化它。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = scanner.nextLine();
        s = s.toUpperCase();
        String[] words = s.split("\\s+");// Split s on whitespace

        // Process each word from words[]
        for (String word : words) {
            int m = word.length(), j;
            for (j = 0; j < word.length(); j++) {
                char c1 = word.charAt(j);
                if (c1 == 'A' || c1 == 'E' || c1 == 'I' || c1 == 'O' || c1 == 'U') {
                    break;
                }
            }
            String translated = word.substring(j, m) + word.substring(0, j);
            System.out.print(translated + "AY" + " ");
        }
    }
}

示例运行:

Enter a sentence: Her food is stolen
ERHAY OODFAY ISAY OLENSTAY

或者,除了使用 String#indexOf​(int ch) ,您可以使用 String#indexOf​(String str, int fromIndex) 把这个句子的所有单词都记下来。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = scanner.nextLine();
        s = s.toUpperCase();

        // Start from index, 0
        int fromIndex = 0, lastPositionOfWhitespace = -1;
        for (int i = 0; i < s.length(); i++) {
            int indexOfWhitespace = s.indexOf(' ', fromIndex);
            String word = "";
            if (indexOfWhitespace != -1) {
                lastPositionOfWhitespace = indexOfWhitespace;
                word = s.substring(fromIndex, indexOfWhitespace);
                fromIndex = indexOfWhitespace + 1;
            } else {
                word = s.substring(lastPositionOfWhitespace + 1);// Last word of the sentence
                i = s.length();// To stop further processing of the loop with counter, i
            }

            int m = word.length(), j;
            for (j = 0; j < word.length(); j++) {
                char c1 = word.charAt(j);
                if (c1 == 'A' || c1 == 'E' || c1 == 'I' || c1 == 'O' || c1 == 'U') {
                    break;
                }
            }
            String translated = word.substring(j, m) + word.substring(0, j);
            System.out.print(translated + "AY" + " ");
        }
    }
}

示例运行:

Enter a sentence: Her food is stolen
ERHAY OODFAY ISAY OLENSTAY

相关问题