【问题标题】:java not getting output piglatinjava没有得到输出piglatin
【发布时间】:2021-03-11 15:46:34
【问题描述】:

以下是将句子的所有单词转换为 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;
        }
         
    }
}

【问题讨论】:

    标签: java bluej


    【解决方案1】:

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

    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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 2018-12-21
      • 2021-12-03
      • 2015-07-13
      • 2017-01-03
      相关资源
      最近更新 更多