0
4

2 回答 2

1

getInvalidResults是试图调用非静态方法的静态invalidCharacters方法。这是不允许的,因为非静态方法需要类的实例。invalidCharacters在这种情况下应该是静态的。

于 2013-11-06T03:52:53.300 回答
0

实际上,这个过程比看起来要容易得多。关键是使用构造函数、getCharResults() 方法和 switch 语句对我有利。

在构造函数的情况下,reNewNumber 声明下方的任何内容都应如下所示:

int i;
containsLetters=getCharResults(newNumberString,length);
if (containsLetters==true){
    for (i=0;i<length;i++){
        //Resets area code to 0 if a character is invalid (aka !@#$%^&* and so on)
        if((Character.isLetterOrDigit(myCharacters [i]))==false){
            areacode=0;
        }
        //Converts every character to a number (stored as a character)
        myCharacters [i]=convertLetNum((myCharacters [i]));
        //Recreates the string in all-number format
        reNewNumber=reNewNumber+myCharacters[i];
            }
    }
    number=Integer.parseInt(reNewNumber);
}

并且 getCharResults() 方法应该扩展为:

public static boolean getCharResults(String newNumString,int getLength){
        //Recreate a character array
        char [] myCharacters=newNumString.toCharArray();
        boolean doesContainLetter=false;
        int j;
        for (j=0;j<getLength;j++){
            //Tests if character is a digit
            if ((Character.isDigit(myCharacters[j])==true)){
                doesContainLetter=false;
            }
            //Tests if character is an illegal character and prints out the required error message
            else if((Character.isLetterOrDigit(myCharacters[j])==false)){
                System.out.println("Error!"+"\t"+newNumString+" contains illegal characters. Any invalid character will be set to zero.");
                doesContainLetter=true;
                return doesContainLetter;
            }
            //Test if the character is a letter
            else{
                doesContainLetter=true;
                return doesContainLetter;
            }
        }
        return doesContainLetter;
}

最后 convertLetNum() 方法应该是:

public char convertLetNum(char letter){
        char returnNum;

其中 returnNum 以char 类型返回一个数字。

此外,我们添加了 case 来测试并返回已经是数字的字符,如下所示:

                case '0': returnNum='0';return returnNum;
                case '1': returnNum='1';return returnNum;
                case '2': returnNum='2';return returnNum;
                case '3': returnNum='3';return returnNum;
                case '4': returnNum='4';return returnNum;
                case '5': returnNum='5';return returnNum;
                case '6': returnNum='6';return returnNum;
                case '7': returnNum='7';return returnNum;
                case '8': returnNum='8';return returnNum;
                case '9': returnNum='9';return returnNum;

请注意,不需要 returnNum,但需要 '#'。

任何额外的代码都是多余的和不必要的。如果在构造函数中正确设置,生成的代码是完美的。

于 2013-11-07T00:35:26.333 回答