计数字母&数学方程式字符串中的单词
因此,我写了一个名为varcount()的方法,如下所示,该方法将计算数学方程中字母变量的数量。
// This method counts the number of variables in the equation
/* Note: If a variable is a word or a substring with length greater than 1,
then count it as a whole */
public int varCount(){
int count = 0;
for (int i = 0; i < getEquation().length(); i++){
if ((getEquation().charAt(i) >= 65 && getEquation().charAt(i) <= 90)
|| (getEquation().charAt(i) >= 97 && getEquation().charAt(i) <= 122)){
count++;
}
}
return count;
}
此方法是在类中制成的,因此此处使用了专用方程式字符串属性的GETTER方法。
一个应该导致的示例是,如果您在字符串 x + 2 = 3 上使用该方法,则会有1个字母变量,该变量为 x ,这很重要。测试后,该方法返回了预期的INT值,并在此范围内起作用。
我真正想要完成的是,如果用户曾经放入诸如 num 之类的变量,那么该方法仍应像第二个评论一样起作用。
在第一次尝试中,我认为,由于它将是一个像 num 的词,那么,如果将上一个字符算作字母,那么整个单词将通过计算出第一个字母来计算,这样的
// This method counts the number of variables in the equation
/* Note: If a variable is a word or a substring with length greater than 1,
then count it as a whole */
public int varCount(){
int count = 0;
for (int i = 0; i < getEquation().length(); i++){
if ((getEquation().charAt(i) >= 65 && getEquation().charAt(i) <= 90)
|| (getEquation().charAt(i) >= 97 && getEquation().charAt(i) <= 122)){
if (i != 0 && (getEquation().charAt(i-1) >= 65 && getEquation().charAt(i-1) <= 90)
|| (getEquation().charAt(i-1) >= 97 && getEquation().charAt(i-1) <= 122)){
continue;
}
count++;
}
}
return count;
}
问题是:该方法仅导致 indexoutofBoundSexception 。
接下来的尝试是用 regex 软件包的修改或替换方法,它使用Matcher Class的GroupCount()方法返回0,就像:
// This method counts the number of variables in the equation
/* Note: If a variable is a word or a substring with length greater than 1,
then count it as a whole */
public int varCount(){
Pattern alphaRange = Pattern.compile("[a-zA-Z]");
Matcher match = alphaRange.matcher(getEquation());
System.out.println(match.groupCount());
return match.groupCount();
}
我缺少或在此方法上错了什么?
So, I have written a method named varCount(), shown below, that would count the number of letter variables in a math equation.
// This method counts the number of variables in the equation
/* Note: If a variable is a word or a substring with length greater than 1,
then count it as a whole */
public int varCount(){
int count = 0;
for (int i = 0; i < getEquation().length(); i++){
if ((getEquation().charAt(i) >= 65 && getEquation().charAt(i) <= 90)
|| (getEquation().charAt(i) >= 97 && getEquation().charAt(i) <= 122)){
count++;
}
}
return count;
}
This method is made inside a class, so the getter method of the private equation string attribute was used here.
One example of what should result from this was, if you are using the method on a string x + 2 = 3, there would be 1 letter variable, which was x, that's counted. After testing, the method returned the expected int value and was functional to that extent.
What I really want to accomplish is, if a user ever puts in a variable like num, then the method should still work like the second comment.
On the first attempt, I thought that, since it would be a word like num, then, if the previous character was counted as a letter, then the whole word would be counted by counting just that first letter, like so:
// This method counts the number of variables in the equation
/* Note: If a variable is a word or a substring with length greater than 1,
then count it as a whole */
public int varCount(){
int count = 0;
for (int i = 0; i < getEquation().length(); i++){
if ((getEquation().charAt(i) >= 65 && getEquation().charAt(i) <= 90)
|| (getEquation().charAt(i) >= 97 && getEquation().charAt(i) <= 122)){
if (i != 0 && (getEquation().charAt(i-1) >= 65 && getEquation().charAt(i-1) <= 90)
|| (getEquation().charAt(i-1) >= 97 && getEquation().charAt(i-1) <= 122)){
continue;
}
count++;
}
}
return count;
}
The issue with this was that the method only resulted in an IndexOutOfBoundsException.
The next attempts were either modifications or replacements of the method body with the regex package, which returned 0 using the Matcher class's groupCount() method, like so:
// This method counts the number of variables in the equation
/* Note: If a variable is a word or a substring with length greater than 1,
then count it as a whole */
public int varCount(){
Pattern alphaRange = Pattern.compile("[a-zA-Z]");
Matcher match = alphaRange.matcher(getEquation());
System.out.println(match.groupCount());
return match.groupCount();
}
What am I missing or going wrong about this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我修改了使用Regex软件包的模式和Matcher类的方法,并设法使程序完全按照预期的方式运行。
我没有意识到,如果在逻辑上找到了匹配的匹配,则需要处理groupCount()方法。我还使用了括号,而不是开放式括号来进行更具体的范围。
不过,我提醒的是,有时候我们可能需要另一只眼睛。谢谢你!
Okay, I had modified the method to use the regex package's Pattern and Matcher class and managed to get the program to function exactly as intended.
I had not realized that the groupCount() method needed to be processed if a match was found, logically. I had also used parentheses instead of open brackets for a more specific range.
What I am reminded about, though, is that there could be times when we need another pair of eyes. Thank you!