Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用递归函数计算以大写元音开头的单词数_Java - Fatal编程技术网

Java 使用递归函数计算以大写元音开头的单词数

Java 使用递归函数计算以大写元音开头的单词数,java,Java,下面是我的代码,它计算并显示以大写元音开头的单词数: 函数count()是一个递归函数,用于计算此类单词的数量 import java.util.*; class Check { String str; int w; StringTokenizer S; void InputString() { Scanner sc=new Scanner (System.in); System.out.println("Enter a S

下面是我的代码,它计算并显示以大写元音开头的单词数: 函数count()是一个递归函数,用于计算此类单词的数量

import java.util.*;

class Check
{
    String str; int w; 

    StringTokenizer S; 
    void InputString()
    {
        Scanner sc=new Scanner (System.in);
        System.out.println("Enter a String:");
        str=sc.nextLine();
    }

    void counter(int nWord)
    {                     
        if(nWord<=S.countTokens())
        {
            String word=S.nextToken(); 
            char first=word.charAt(0); 
            if(first=='A' || first=='E' || first=='I' || first=='O' || first=='U')
            //Checking if the word begins with a CAPITAL VOWEL.
            {
                w++; 
            }
            counter(nWord+1);
        }
    }

    void display()
    {
        S=new StringTokenizer(str);
        counter(1);
        System.out.println("Given String: "+str);
        System.out.println("Number of words beginning with capital vowels = "+w);
    }

    void main()
    {
        InputString();
        display();
    }
}
import java.util.*;
课堂检查
{
字符串str;int w;
字符串标记器;
void InputString()
{
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入字符串:”);
str=sc.nextLine();
}
无效计数器(int nWord)
{                     

如果(nWordJavadoc谈到
StringTokenizer.countTokens()

计算此标记器的nextToken方法 可以在生成异常之前调用。当前位置 它并不先进

换句话说,在每次调用
nextToken
后,
countTokens
将返回一个较低的数字。
这会导致调用
计数器的频率低于预期


这是因为S.countTokens()返回标记器中剩余的标记数。最初,“Java是一种面向对象的编程语言”中有7个标记。当您调用
S.nextToken()
时,它将删除“Java”从列表中,在下一次调用
S.countTokens()
时,我们得到6个(而不是7个,因为Java不再在令牌列表中)

循环在您读取了4个令牌后结束,只剩下3个令牌。在这个阶段,
nWord=4
但是
S.countTokens()=3
因此递归停止在这里,您根本不需要“面向编程语言”这个词

要解决此问题,请首先在display()函数本身中计算令牌数,并将其存储在变量nTokens中。然后检查条件变为:

if(nWord <= nTokens)

if(nWord类没有计算输入的所有单词,这就是数字为off的原因

当您在
count()
中打印参数
nWord
S.countTokens()
的返回值时,您将看到
nWord
增加时,
countTokens()的返回值
随着每次调用而减少,直到小于
nWord
,这将在查看所有令牌之前停止递归

countTokens()函数返回剩余令牌的数量,而不是令牌的总数,因此当
nextToken()
使用令牌时,它会减少

您可以使用
StringTokenizer
的首选函数
hasMoreTokens()
,查看是否有更多令牌需要处理,然后结果将是正确的:

...
if(S.hasMoreTokens())
{
    String word=S.nextToken();
...

Enter a String:
Java Is An Object Oriented Programming Language.
Given String: Java Is An Object Oriented Programming Language.
Number of words beginning with capital vowels = 4

我明白了。我不知道。它现在按预期工作。谢谢!我现在拿到了。谢谢!
Enter a String:
Java Is An Object Oriented Programming Language.
nWord=1 countTokens=7
Java
nWord=2 countTokens=6
Is
nWord=3 countTokens=5
An
nWord=4 countTokens=4
Object
nWord=5 countTokens=3
Given String: Java Is An Object Oriented Programming Language.
Number of words beginning with capital vowels = 3
...
if(S.hasMoreTokens())
{
    String word=S.nextToken();
...

Enter a String:
Java Is An Object Oriented Programming Language.
Given String: Java Is An Object Oriented Programming Language.
Number of words beginning with capital vowels = 4