从java

时间:2016-09-10 04:39:06

标签: java string

那里。我需要一些帮助。我正在处理用户输入文本,反转文本,然后读取文本中的元音数量,并告诉他们反向文本中有多少元音。

public static void main(String[] args)
{
 System.out.println ("Please type in whatever you want ");
 Scanner type_input = new Scanner (System.in);
 Scanner type_input = new Scanner (System.in);
 StringBuilder type_output = new StringBuilder();
 type_output.append(type_hold);
 type_output=type_output.reverse();
 System.out.println("Is this what you types in? " + type_output);   

 for(int vowel_num = 0; vowel_num< type_hold.length(); vowel_num++)
    {
        if((type_hold.charAt(vowel_num) =='a')||(type_hold.charAt(vowel_num) =='e')||
            (type_hold.charAt(vowel_num) =='o')||(type_hold.charAt(vowel_num) =='i')||
            (type_hold.charAt(vowel_num) =='u')){    

        System.out.println("There are " + vowel_num + " vowels in " + type_hold);
    }

但是当我输入它来运行它时,我得到以下内容。我不确定我搞砸了哪里。

run:
Please type in whatever you want 
hello
Is this what you types in? olleh
There are 1 vowels in hello
There are 4 vowels in hello

编辑:我明白了。感谢大家的帮助!

3 个答案:

答案 0 :(得分:3)

  1. 什么是type_hold?没看到你实例化它,而你正在使用它。
  2. vowel_num是什么?你在循环字符串的索引?或你计算的元音数量?
  3. 你应该做些什么来计算字符串中的元音:

    假设index是我们当前正在扫描的字符串的索引,vowel_count是您遇到的元音数。

    int vowel_count = 0;
    for(int index = 0; index < type_hold.length(); index++) {
        if((type_hold.charAt(index) =='a') || 
           (type_hold.charAt(index) =='e') || 
           (type_hold.charAt(index) =='o') || 
           (type_hold.charAt(index) =='i') || 
           (type_hold.charAt(index) =='u')){
    
             // character at vowel_index is a vowel.
             // you have encountered 1 more vowel! 
             System.out.println("Character at " + index + " is a vowel.");
             vowel_count ++;
         }
    }
    
    System.out.println("There are " + vowel_count + " vowels in " + type_hold);
    

答案 1 :(得分:3)

Java 8流可用于String内的字符数组。

StringBuilder type_hold = new StringBuilder();
long vowel_num = type_hold.toString().toLowerCase().chars()
        .filter(it -> "aeiou".indexOf(it) != -1).count();

答案 2 :(得分:-1)

稍微修改了你的代码;看看;

public static void main(String[] args)
{
  System.out.println ("Please type in whatever you want ");
  Scanner type_input = new Scanner (System.in);
  Scanner type_input = new Scanner (System.in);
  StringBuilder type_output = new StringBuilder();
  type_output.append(type_hold);
  type_output=type_output.reverse();
  System.out.println("Is this what you types in? " + type_output);   

  int count=0;
  for(int vowel_num = 0; vowel_num< type_hold.length(); vowel_num++)
  {
    if((type_hold.charAt(vowel_num) =='a') ||(type_hold.charAt(vowel_num) =='e')||
        (type_hold.charAt(vowel_num) =='o')||(type_hold.charAt(vowel_num) =='i')||
        (type_hold.charAt(vowel_num) =='u')){    

    //System.out.println("There are " + vowel_num + " vowels in " + type_hold);                      Instead of this

    count++;
   }

System.out.println("There are " + count + " vowels in " + type_hold);
}

这应该完成你的工作。