【问题标题】:Java - Converting the characters in a string array depending on whether or not it's a vowelJava - 根据是否为元音转换字符串数组中的字符
【发布时间】:2013-02-20 17:25:32
【问题描述】:

该方法接受任何名称并测试字符是元音还是辅音。如果是元音,则将字符变为大写,如果是辅音,则将字符变为小写。有什么想法吗?我不知道如何在 if else 语句中添加.toUpperCase.toLowerCase

public static void parsing(String name[])
{
    String temp = name[0];

    int i = 0;
    for(i = 0; i < temp.length(); i++)
    {
        if(temp.charAt(i) == 'a' || temp.charAt(i) == 'A' ||
           temp.charAt(i) == 'e' || temp.charAt(i) == 'E' ||
           temp.charAt(i) == 'i' || temp.charAt(i) == 'I' ||
           temp.charAt(i) == 'o' || temp.charAt(i) == 'O' ||
           temp.charAt(i) == 'u' || temp.charAt(i) == 'U')
        {
         System.out.print(temp.charAt(i).toUpperCase);
        }//Obviously wrong but I don't know what to do.
            else
            {
            System.out.print(temp.charAt(i).toLowerCase);
            }//Obviously wrong but I don't know what to do.     
    }   

【问题讨论】:

  • 那么当你运行它时会发生什么?
  • String.equalsIgnoreCase 是你的朋友

标签: java string character


【解决方案1】:

要转换单个字符,请使用 Character 类中的方法:

System.out.print(Character.toUpperCase(temp.charAt(i)));
System.out.print(Character.toLowerCase(temp.charAt(i)));

【讨论】:

  • 这很完美,并且比其他答案短得多。谢谢,约翰。
【解决方案2】:

创建两个最终数组 - 一个带有元音,第二个带有辅音。然后检查循环中的当前字符是元音还是辅音,并进行适当的更改。

【讨论】:

    【解决方案3】:

    由于 String 是不可变的,因此您正在头疼。重建生成的字符串。 一个(位次优的)解决方案是:

    public static void parsing(String[] names)
    {
        for (int i = 0; i < names.length; ++i) {
            names[i] = chAngEd(names[i]);
        }
    }
    
    private static String chAngEd(String s) {
        String result = "";
        for (int i = 0; i < s.length(); ++i) {
            char ch = s.charAt(i);
            if (ch == 'a' || ...) {
                ch = Character.toUpperCase(ch);
            } else {
                ch = ...
            }
            result += ch;
        }
        return result;
    }
    

    【讨论】:

      【解决方案4】:
      public static void parsing(String names[]){
         for (int i=0; i<names.length; ++i){
             names[i] = capitaliseConsts(names[i]);
         }
      }
      
      
      private static String capitaliseConsts(String name){
          StringBuilder sb = new StringBuilder();
          Character c;
      
          for (int i=0; i<name.length(); ++i){
              c = name.charAt(i);
              if (c.equalsIgnoreCase('a') || 
                  c.equalsIgnoreCase('e') ||
                  c.equalsIgnoreCase('i') ||
                  c.equalsIgnoreCase('o') ||
                  c.equalsIgnoreCase('u')){
      
                sb.append(Character.toUpperCase(c));
             }
             else{
                   sb.append(Character.toLowerCase(c));
             }
          }
          return sb.toString();
      }
      

      【讨论】:

        【解决方案5】:
        String vowelsArray = "aeiuo";
        String constantsArray = "uppercase constants";
        int stringLength = name.length();
        String givenNameCopy = name.ToString();
        for(int i = 0; i < stringLength; i++){
        if(vowelsArray.contains(givenNameCopy[i]))
           then uppercase
        else if(constantsArray.contains(givenNameCopy[i]))
           then lowercase
        else
            continue;
        

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 2018-09-28
          • 1970-01-01
          • 1970-01-01
          • 2023-03-19
          • 1970-01-01
          • 2012-04-17
          • 2012-10-20
          相关资源
          最近更新 更多