gpt4 book ai didi

java - 在 java 中从 String 中读取元音的数量

转载 作者:行者123 更新时间:2023-11-29 07:33:08 25 4
gpt4 key购买 nike

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

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

编辑:我想通了。感谢大家的帮助!

最佳答案

  1. 什么是type_hold?没看到你实例化了,你在用。
  2. 什么 vowel_num?您正在遍历字符串的索引?还是你数过的元音数量?

计算字符串中的元音字母应该怎么做:

假设 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);

关于java - 在 java 中从 String 中读取元音的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39422616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com