Java:字符串中的元音数量

huangapple go评论55阅读模式
英文:

Java: Number of vowel in a string

问题

这是要翻译的内容:

我被要求编写一个名为NumberOcc的类,其中包含以下方法:

  • 方法getNbOcc,它接受一个字符串str和一个字符'c'作为参数,返回字符'c'出现的次数。
  • 方法dspNbOcc,显示getNbOcc返回的值。
  • 方法getNbVoy,返回字符串str中元音字母的数量。
  • 方法dspNbVoy,显示getNbVoy返回的值。

问题在于getNbVoy返回的值是错误的,例如:对于str = "stackexchange",它返回34个元音字母。

以下是代码:

public class NumberOcc {
    static int count1=0;
    static int count2=0;

    public static int getNbOcc(String str, char c) {
        for(int i=0; i<str.length(); i++) {
            if (str.charAt(i) == c)
                count1++;
        }
        return count1;
    }

    public static void dspNbOcc() {
        System.out.println(count1);
    }

    public static int getNbVoy(String str) {
        String vowel = "aeiouy";

        for(int j=0; j<vowel.length(); j++) {
            count2 += getNbOcc(str, vowel.charAt(j));
        }
        return count2;
    }

    public static void dspNbVoy() {
        System.out.println(count2);
    }
}

测试类:

public class TestNumberOcc {
    public static void main(String[] args) {
        String str = "stackexchange";
        NumberOcc.getNbOcc(str, 'e');
        NumberOcc.dspNbOcc();
        NumberOcc.getNbVoy(str);
        NumberOcc.dspNbVoy();
    }
}

谢谢你的帮助。

英文:

I was asked to write a class NumberOcc with these methods:

-method getNbOcc which takes as arguments a string str and a character 'c' and return the number of occurence of the character 'c'.

-method dspNbOcc which displays the value returned by getNbOcc

-method getNbVoy which returns the number of vowel inside a string str

-method dspNbVoy which displays the value returned by getNbVoy

The problem is the value returned by getNbVoy is wrong, example: for str=stackexchange it returns 34 vowels.

public class NumberOcc {
static int count1=0;
static int count2=0;
public static int getNbOcc(String str, char c) {
	for(int i=0;i&lt;str.length();i++) {
		if (str.charAt(i)==c) 
				count1++;}
return count1;
    }
public static void dspNbOcc() {
	System.out.println(count1);
}
public static int getNbVoy(String str) {
	String vowel=&quot;aeiouy&quot;;
	
	for(int j=0;j&lt;vowel.length();j++) { 
	
		count2+=getNbOcc(str,vowel.charAt(j));}
		return count2;
  }
public static void dspNbVoy() {
	System.out.println(count2);
}
}

TestClass

public class TestNumberOcc {

	public static void main(String[] args) {
    String str=&quot;stackexchange&quot;;
    NumberOcc.getNbOcc(str, &#39;e&#39;);
    NumberOcc.dspNbOcc();
    NumberOcc.getNbVoy(str);
    NumberOcc.dspNbVoy();
		
		

	}

}

Thanks for helping

答案1

得分: 2

移除static字段,将值传递给方法,并使用它们来显示结果。例如,

public static int getNbOcc(String str, char c) {
    int count = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) == c) {
            count++;
        }
    }
    return count;
}

public static void dspNbOcc(String str, char c) {
    System.out.println(getNbOcc(str, c));
}

public static int getNbVoy(String str) {
    int count = 0;
    char[] vowels = "aeiouy".toCharArray();
    for (char ch : vowels) {
        count += getNbOcc(str.toLowerCase(), ch);
    }
    return count;
}

public static void dspNbVoy(String str) {
    System.out.println(getNbVoy(str));
}

然后,测试所有功能就像这样简单:

public static void main(String[] args) {
    String str = "stackexchange";
    NumberOcc.dspNbOcc(str, 'e');
    NumberOcc.dspNbVoy(str);
}
英文:

Remove the static fields, pass the values to the methods. And use them to display the results. Like,

public static int getNbOcc(String str, char c) {
	int count = 0;
	for (int i = 0; i &lt; str.length(); i++) {
		if (str.charAt(i) == c) {
			count++;
		}
	}
	return count;
}

public static void dspNbOcc(String str, char c) {
	System.out.println(getNbOcc(str, c));
}

public static int getNbVoy(String str) {
	int count = 0;
	char[] vowels = &quot;aeiouy&quot;.toCharArray();
	for (char ch : vowels) {
		count += getNbOcc(str.toLowerCase(), ch);
	}
	return count;
}

public static void dspNbVoy(String str) {
	System.out.println(getNbVoy(str));
}

And then testing everything is as simple as

public static void main(String[] args) {
	String str = &quot;stackexchange&quot;;
	NumberOcc.dspNbOcc(str, &#39;e&#39;);
	NumberOcc.dspNbVoy(str);
}

答案2

得分: 0

问题在于你没有在 count 方法的开头初始化 count1(以及 count2,但那个 bug 在这种情况下不会影响任何东西)... 在你的 getNbOcc 方法循环之前添加以下行:

public static int getNbOcc(String str, char c) {
    count1 = 0;  // 添加这行
    for(int i=0; i<str.length(); i++) {
英文:

the issue is you're not initializing your count1 (nor count2, but that bug doesn't affect anything in this case) at the beginning of your count method... add this line to the beginning of your getNbOcc method before the loop:

public static int getNbOcc(String str, char c) {
    count1 = 0;  // add this line
    for(int i=0;i&lt;str.length();i++) {

答案3

得分: 0

解决方案是将你在 countLetterInString 函数中所做的应用到 countVowelsInString 函数中。只需记得使用局部变量。如果函数被多次调用,使用静态/全局变量会遇到问题,但局部变量每次都会以相同的方式工作。

public static int countVowelsInString(String str) {
    String vowels = "aeiouy";

    // 将计数器移到函数内部
    int numVowels = 0;
    
    for (int j = 0; j < vowels.length(); j++) {
        numVowels += getNbOcc(str, vowels.charAt(j));
    }
    return numVowels;
}
英文:

The solution is to apply what you did in your countLetterInString function to countVowelsInString. Just remember to use local variables. You will run into issues with static/global variables if the function is called more than once, but local variables will work the same way every time.

public static int countVowelsInString(String str) {
    String vowels = &quot;aeiouy&quot;;

    // Move counter into the function
    int numVowels = 0;
    
    for(int j = 0;j&lt;vowel.length();j++) {
        numVowels += getNbOcc(str, vowel.charAt(j));
    }
    return numVowels;
}

huangapple
  • 本文由 发表于 2020年10月5日 07:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64200665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定