0

就在我认为我已经掌握了 Java 的基础知识时(已经整整一个学期了!),发生了一些事情让我对此产生了疑问。我正在做一些练习复习并且有一点问题。这是代码:

public class LetterCount 
{
    private char[] wordArray;
    private int numVowels = 0, numConsonants = 0, numSpaces = 0, numDigits = 0;

    public LetterCount(String str)
    {
        wordArray = str.toCharArray();
    }

    public int getNumVowels()
    {
        for (int count = 0; count < wordArray.length; count++)
        {
           if (wordArray[count] == 'a' || wordArray[count] == 'e' || 
               wordArray[count] == 'i' || wordArray[count] == 'o' || wordArray[count] 
               == 'u' || wordArray[count] == 'y')

           numVowels++;
        }
        return numVowels;
    }

    public int getNumDigits()
    {
        for (int count = 0; count < wordArray.length; count++)
        {
            if (Character.isDigit(wordArray[count]))
                numDigits++;
        }
        return numDigits;
    }

    public int getWhiteSpace()
    {
        for (int count = 0; count < wordArray.length; count++)
        {
            if (Character.isSpaceChar(wordArray[count]))
                numSpaces++;
        }

        return numSpaces;
    }

    public int getNumConsonants()
    {       
        numConsonants = wordArray.length - getNumVowels() - getNumDigits() - getWhiteSpace();

        return numConsonants;
    }

    public String toString()
    {
        String str = "Characters: " + wordArray.length + "\n" +
    "Vowels: " + getNumVowels() + "\n" +
                "Consonants: " + getNumConsonants() + "\n" +
                "Digits: " + getNumDigits() + "\n" +
                "Spaces: " + getWhiteSpace();

        return str;
    }
}

这是输出:

Enter a sentence: this is a test 4 u
Characters: 18
Vowels: 5
Consonants: 2
Digits: 2
Spaces: 10

我的问题: 1) 我确信我可以在 toString() 方法中使用字段名称(例如numVowelsgetNumConsonants()),但似乎这个类需要我使用方法名称。当我使用字段名称时,我得到 0。为什么有区别?我知道如果我返回一个方程,我必须使用方法名称。

2) 我也不明白为什么我的 numConsonants() 方法没有返回正确的数字。如果我分别返回每个字段(并调用该方法,因为我无法调用字段名称),我会得到正确的数字。把它们放在一个等式中,这是不正确的。我究竟做错了什么?

这是原始的主要方法。我已经对其进行了编辑以调用 LetterCount 类中的方法:

    import java.util.Scanner;


public class LetterCountDemo {

    public static void main(String[] args) 
    {
        String sentence;
        LetterCount lc;
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a sentence: ");
        sentence = keyboard.nextLine();

        lc = new LetterCount(sentence);

        System.out.println(lc);
    }

}

添加:

        lc.getNumVowels();
    lc.getNumConsonants();
4

3 回答 3

2

You can use the 'field' (it's known as a 'member variable'), but in your case, it's initialized to zero, and only updated by the method.

So, if you never call the method, the member variable is never updated, so remains at zero.

Also, you don't 'call field names' - you can call a method, which may return a value. You can reference that value, and similarly you can reference a member variable.

于 2012-12-07T03:43:59.713 回答
1

The answer to your first question is relatively simple. The fields are zero until the methods are called at least once. In other words, the fields are set in the methods so you have to call the methods first.

The answer to the second question is related. When you call getNumVowels, getNumDigits, or getWhitespace your are increasing the field values. If you initialize them to zero at the beginning, your approach will work.

于 2012-12-07T03:44:40.050 回答
0

您必须使用方法而不是字段的原因很简单。您的方法不是简单的访问器方法。(检查此链接)它们会进行一些计算并更新归档变量并返回它们。因此,在您调用该方法之前,您的字段的值为零。

这种更新字段变量的方式很糟糕。如果您两次调用该方法(例如:)getNumVowels,它将更新已更新的字段变量numVowels

问题与getNumConsonants上述答案有关。

这里的解决方案是不使用字段变量并使用局部变量。请参见下面的示例。

public int getNumDigits()
{
    int numDigits = 0;
    for (int count = 0; count < wordArray.length; count++)
    {
        if (Character.isDigit(wordArray[count]))
            numDigits++;
    }
    return numDigits;
}

使用此修改getNumDigits()将始终返回正确答案。

于 2012-12-07T03:55:48.697 回答