Java 如何获取字符串中的单个字母
我对如何在字符串数组中搜索单个字母(特定元音)感到非常困惑Java 如何获取字符串中的单个字母,java,Java,我对如何在字符串数组中搜索单个字母(特定元音)感到非常困惑 import static java.lang.System.*; import java.util.Arrays; public class Word { private String word; private static String vowels = "AEIOUaeiou"; //only one public Word() { String vow = "";
import static java.lang.System.*;
import java.util.Arrays;
public class Word {
private String word;
private static String vowels = "AEIOUaeiou"; //only one
public Word() {
String vow = "";
vowels = vow;
}
public Word(String wrd) {
word=wrd;
}
public void setWord(String wrd) {
}
public int getNumVowels(String[] ray, String vowels) {
int count=0;
for(String item : ray) {
if() {
count = count + 1;
}
}
return count;
}
public int getLength(String[] ray) {
return 0;
}
public String toString(String[] ray) {
return "";
}
}
这是跑步者。如何通过输入搜索单个字母来获取NumVowels
import java.util.Arrays;
import java.util.Scanner;
import static java.lang.System.*;
public class WordRunner {
public static void main(String[] args) {
String alpha="";
String [] ray = new String[4];
ray[0] ="Polo";
ray[1] ="Crazy";
ray[2] ="Abracadabra";
ray[3] ="Awesome";
out.println(Arrays.toString(ray));
out.println("Number of vowels: " + Arrays.getNumVowels(ray));
out.println("Length of the word: " + Arrays.getLength(ray));
}
}
您可以从字符串中获取char数组,并在其上循环查找所需的元音(如果要计算单个元音出现的次数,则无需传递字符串,只需使用char即可)
public int getNumVowels(字符串[]ray,字符元音){
整数计数=0;
用于(字符串s:ray)
{
char[]数组=s.toLowerCase().toCharArray();
对于(int i=0;i直接方式:
String vowels = "AEIOUaeiou";
int count = 0;
for (int i=0; i<str.length(); i++)
{
if (vowels.contains(str.charAt(i) + ""))
{
count++;
}
}
return count;
String元音=“AEIOUaeiou”;
整数计数=0;
对于(int i=0;i,您的代码中有几个错误
您正在调用不存在的数组方法
out.println("Number of vowels: "+ Arrays.getNumVowels(ray));
out.println("Length of the word: "+ Arrays.getLength(ray));
修改Word
class(你放了很多不必要的东西):
更新:如果希望word类在数组中计算元音,只需使用现有方法即可
在Word
类中,添加接收数组并使用旧数组的现有方法的重载:
// overload: same method name, but different arguments!!!
public static int getNumVowels(String[] array)
{
int count = 0;
for (i = 0; i < array.lenght; i ++){
// use existing method to save code!!
count += getNumVowels(array[i]);
}
return count;
}
他不想计算所有元音,但只想计算我使用该方法时某个特定元音出现的次数,但现在它说contains不适用于该语句…真的,对不起,请使用if(vouels.contains(word.charAt(i)+“”)
将char转换为stringGreat听到这个消息太好了!:)这是您在StAdExfFor中的第一个问题,您必须知道这个或任何答案是否已经解决了您的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,您已经找到了解决方案并给回答者和您自己带来了一些声誉。当然,没有义务这样做。
import static java.lang.System.*;
import java.util.Arrays;
public class Word
{
private static String vowels = "AEIOUaeiou"; //only one
public static int getNumVowels(String word)
{
int count=0;
for (int i=0; i < word.length(); i++)
{
// check if selected char is a vowel
if (vowels.contains(word.charAt(i) + ""))
{
count ++; // same as count = count + 1
}
}
return count;
}
}
public static void main(String[] args)
{
System.out.println("Number of vowels of Polo: "+ Word.getNumVowels("Polo"));
//Output:
// Number of vowels of Polo: 2
// or execute like this:
String a = "Polo";
int vowels = Word.getNumVowels(a);
System.out.printlm("Number of vowels of Polo: "+ vowels);
//Output:
// Number of vowels of Polo: 2
}
// overload: same method name, but different arguments!!!
public static int getNumVowels(String[] array)
{
int count = 0;
for (i = 0; i < array.lenght; i ++){
// use existing method to save code!!
count += getNumVowels(array[i]);
}
return count;
}
String [] ray = new String[4];
ray[0] ="Polo";
ray[1] ="Crazy";
ray[2] ="Abracadabra";
ray[3] ="Awesome";
int vowels = Word.getNumVowels(ray); // count vowels in ALL array words...