Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用字符串方法查找和计算字符串中的元音?_Java_String - Fatal编程技术网

Java 使用字符串方法查找和计算字符串中的元音?

Java 使用字符串方法查找和计算字符串中的元音?,java,string,Java,String,我的作业有这个问题(老实说,至少我不想隐瞒) 我在想怎么做时遇到了问题 给出以下声明:String-phrase=“WazzUp?-谁是第一名?”-IDUNNO”;编写必要的代码以 计算字符串中的元音数,并在屏幕上打印相应的消息 以下是我目前掌握的代码: String phrase = " WazzUp ? - Who's On FIRST ??? - IDUNNO"; int i, length, vowels = 0; String j; length = phrase.length();

我的作业有这个问题(老实说,至少我不想隐瞒) 我在想怎么做时遇到了问题

给出以下声明:String-phrase=“WazzUp?-谁是第一名?”-IDUNNO”;编写必要的代码以 计算字符串中的元音数,并在屏幕上打印相应的消息

以下是我目前掌握的代码:

String phrase =  " WazzUp ? - Who's On FIRST ??? - IDUNNO";
int i, length, vowels = 0;
String j;
length = phrase.length();
for (i = 0; i < length; i++)
{

  j = phrase.substring(i, i++);
  System.out.println(j);

  if (j.equalsIgnoreCase("a") == true)
    vowels++;
  else if (j.equalsIgnoreCase("e") == true)
    vowels++;
  else if (j.equalsIgnoreCase("i") == true)
    vowels++;
  else if (j.equalsIgnoreCase("o") == true)
    vowels++;
  else if (j.equalsIgnoreCase("u") == true)
    vowels++;

}
System.out.println("Number of vowels: " + vowels);
String-phrase=“WazzUp?-谁先上场?”-IDUNNO”;
int i,长度,元音=0;
字符串j;
长度=短语。长度();
对于(i=0;i
然而,当我运行它时,它只会生成一堆空行。有人能帮忙吗?

短语.子字符串(i,i++)应该是
短语。子字符串(i,i+1)

i++
给出
i
的值,然后将其加1。正如您现在看到的,
字符串j
实际上是
短语.substring(i,i),始终为空字符串


您不需要更改
for
循环体中
i
的值,因为它在
for(i=0;i
中已经增加了
i

在使用is之后,i++增加了i,所以本质上说是string.substring(i,i)。由于结束标记是独占的,因此将始终返回空字符串。一个简单的解决办法就是把它改成

j = phrase.substring(i, ++i);

希望有帮助

我认为没有必要在循环中使用print语句

String s = "Whatever you want it to be.".toLowerCase();
int vowelCount = 0;
for (int i = 0, i < s.length(); ++i) {
    switch(s.charAt(i)) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            vowelCount++;
            break;
        default:
            // do nothing
    }
}
String s=“不管你想要什么。”.toLowerCase();
int-vouelcount=0;
对于(int i=0,i

这将字符串转换为小写,并检查字符串中的所有字符的元音。

公共类JavaApplication2{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    // TODO code application logic here
    System.out.println("Enter some text");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine().toLowerCase();

    char[] vowel = new char[]{'a', 'e', 'i', 'o', 'u'};
    int[] countVowel = new int[5];
    for (int j = 0; j < input.length(); j++) {
        char c =input.charAt(j);
        if(c=='a')
            countVowel[0]++;
        else if(c=='e')
            countVowel[1]++;
        else if(c=='i')
            countVowel[2]++;
        else if(c=='o')
            countVowel[3]++;
        else if(c=='u')
            countVowel[4]++;


    }
     for (int i = 0; i <countVowel.length; i++) {
            System.out.println("Count of vowel " + vowel[i] + "=" + countVowel[i]);
        }

    }
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发IOException{
//此处的TODO代码应用程序逻辑
System.out.println(“输入一些文本”);
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
字符串输入=br.readLine().toLowerCase();
字符[]元音=新字符[]{'a','e','i','o','u'};
int[]countVowel=新int[5];
对于(int j=0;j对于(int i=0;i这可以/应该是一条直线

System.out.println("the count of all vowels: " + (phrase.length() - phrase.replaceAll("a|e|i|o|u", "").length()));

及其字符串唯一的方法

在上面的一个答案中改进考虑上限的元音:

System.out.println(s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length());
使用集合:

     String word = "busiunesuse";
     char[] wordchar= word.toCharArray();
     Character allvowes[] ={'a','e','i','o','u'};
     Map<Character, Integer> mymap = new HashMap();

     for(Character ch: wordchar)
     {
         for(Character vowels : allvowes)
         {
            if(vowels.equals(ch))
            {
             if(mymap.get(ch)== null)
             {
                 mymap.put(ch, 1);
             }
             else
             {
                 int val = mymap.get(ch);
                 mymap.put(ch, ++val);
             }
            }

         }
     }

     Set <Character> myset = mymap.keySet();
     Iterator myitr = myset.iterator();

     while(myitr.hasNext())
     {
         Character key = (Character) myitr.next();
         int value = mymap.get(key);
         System.out.println("word:"+key+"repetiotions:"+value);


     }


}
String word=“businessuse”;
char[]wordchar=word.tocharray();
字符所有元音[]={'a','e','i','o','u'};
Map mymap=newhashmap();
for(字符ch:wordchar)
{
for(字符元音:所有元音)
{
if(元音等于(ch))
{
if(mymap.get(ch)==null)
{
mymap.put(ch,1);
}
其他的
{
int val=mymap.get(ch);
mymap.put(ch,+val);
}
}
}
}
设置myset=mymap.keySet();
迭代器myitr=myset.Iterator();
while(myitr.hasNext())
{
Character key=(Character)myitr.next();
int value=mymap.get(键);
System.out.println(“字:“+key+”重复:“+value”);
}
}
}我知道这是一个老版本, 但我认为如果元音使用and和数组会更好:

publicstaticvoidmain(字符串[]args){
字符串短语=“WazzUp?-谁是第一个?”-IDUNNO.toLowerCase();
int元音=0;
字符串[]数组={“a”、“e”、“i”、“o”、“u”};
for(int i=0;i
计算字符串中元音的数量:

class Test {  

      static int a;

      public static String countVowel(String strName) { 

          char ch[] = strName.toCharArray();


          for(int i=0; i<ch.length; i++) {

            switch(ch[i]) {

            case 'a':

             a++;   

            break;      

            case 'e':
                a++;    

                break;

            case 'i':
                 a++;   

                break;

            case 'o':
                 a++;   

                break;

            case 'u':
                 a++;   

                break;

            }

          }

          strName = String.valueOf(a);

          return strName;       
      } 

      public static void main(String[] args) {  

        Scanner s = new Scanner(System.in);
        System.out.print(countVowel(s.nextLine())); 
     }   

  } 
类测试{
静态int a;
公共静态字符串countVowel(字符串strName){
char ch[]=strName.toCharArray();

对于(int i=0;i我知道我有点晚了,但是如果其他人使用口音的话,这个回答可能会对他们有帮助

为了避免出现问题,我们可以将输入文本规范化为NFD,通过NFD将字母和重音中的重音(be:umlauts、grave重音、acute重音、扬抑重音等)分开

示例:
“schön”--NFD-->“scho\u0308n”

解决办法是

  • 将文本规范化为,并将重音替换为()
  • 将文本转换为小写
  • 替换所有不是元音的东西(或任何你想数的东西)
  • 导入java.text.Normalizer;
    公共班机{
    private static String str=“这是带vòwêls的文本”;
    公共静态void main(字符串[]args){
    //规范化文本
    //替换所有的口音
    //转换成小写
    String text=Normalizer.normalize(str,Normalizer.Form.NFD)
    
    class Test {  
    
          static int a;
    
          public static String countVowel(String strName) { 
    
              char ch[] = strName.toCharArray();
    
    
              for(int i=0; i<ch.length; i++) {
    
                switch(ch[i]) {
    
                case 'a':
    
                 a++;   
    
                break;      
    
                case 'e':
                    a++;    
    
                    break;
    
                case 'i':
                     a++;   
    
                    break;
    
                case 'o':
                     a++;   
    
                    break;
    
                case 'u':
                     a++;   
    
                    break;
    
                }
    
              }
    
              strName = String.valueOf(a);
    
              return strName;       
          } 
    
          public static void main(String[] args) {  
    
            Scanner s = new Scanner(System.in);
            System.out.print(countVowel(s.nextLine())); 
         }   
    
      }