两种方法的组合不起作用 - Java

时间:2016-06-19 08:32:00

标签: java

在本课程中,我从用户那里获得了一个字符串值。这有两个功能。

  • 根据输入的字长我想分配点/标记(例如:如果用户输入的长度是3,我想分配3个点)。
  • 此外,每个字母都有不同的分值。

    作为最终输出,我想显示结合这两种方法的分数。 例如:(如果用户输入' APPLE',最终得分应为=字母点(1 + 3 + 3 + 1 + 1)+字长点数(9) 总数= 9 + 9 = 18。

我试过这个,但最后得分并没有产生。请帮我解决这个问题。

public void ScoreWord()
{
for(i=0; i < word.length();i++);
    {
wordchar = word.charAt(i-1);
lettervalue=0;

switch(wordchar)
{
        case 'A': case 'E': case 'I': case 'L': case 'N': case 'O': case 'R':  case 'S': case 'T': case 'U':
        lettervalue= 1;
        break;

        case 'D': case 'G':
        lettervalue=2;
        break;

        case 'B': case 'C': case 'M': case 'P':
        lettervalue=3;
        break;

        case 'F': case 'H': case 'Y': case 'V': case 'W':
        lettervalue=4;
        break;

        case 'K':
        lettervalue=5;
        break;

        case 'J': case 'X':
        lettervalue=8;
        break;

        case 'Q': case 'Z':
        lettervalue=10;
        break;
}


    if(words.length == 1)
    {
        scores= lettervalue+wordlength_score[0];
    }
    if(words.length == 2)
    {
        scores= lettervalue+wordlength_score[1];
    }
    if(words.length == 3)
    {
        scores= lettervalue+wordlength_score[2];
    }
    if(words.length == 4)
    {
        scores= lettervalue+wordlength_score[3];
    }
    if(words.length == 5)
    {
        scores= lettervalue+wordlength_score[4];
    }
    if(words.length == 6)
    {
        scores= lettervalue+wordlength_score[5];
    }
    if(words.length == 7)
    {
        scores= lettervalue+wordlength_score[6];
    }
    if(words.length == 8)
    {
        scores= lettervalue+wordlength_score[7];
    }
    if(words.length == 9)
    {
        scores= lettervalue+wordlength_score[8];
    }
    if(words.length == 10)
    {
        scores= lettervalue+wordlength_score[9];
    }
    if(words.length == 11)
    {
        scores= lettervalue+wordlength_score[10];
    }
    if(words.length == 12)
    {
        scores= lettervalue+wordlength_score[11];
    }

words[0]=word;                          

}

1 个答案:

答案 0 :(得分:0)

首先,您必须创建一个方法,将字符串(word)作为参数,并根据后者返回分数:

public int letterScore(String word)
{
    int lettervalue = 0;
    char wordchar;
    for(int i=0; i < word.length();i++)//what was this ; here?
    {
        wordchar = word.charAt(i);

        switch(wordchar)
        {
            case 'A': case 'E': case 'I': case 'L': case 'N': case 'O': case 'R':      case 'S': case 'T': case 'U':
            lettervalue+= 1;
            break;
            case 'D': case 'G':
            lettervalue+=2;
            break;
            case 'B': case 'C': case 'M': case 'P':
            lettervalue+=3;
            break;
            case 'F': case 'H': case 'Y': case 'V': case 'W':
            lettervalue+=4;
            break;
            case 'K':
            lettervalue+=5;
            break;
            case 'J': case 'X':
            lettervalue+=8;
            break;
            case 'Q': case 'Z':
            lettervalue+=10;
            break;        
       }



    }
    return lettervalue;
}

现在举例来说,可以像这样计算单词的得分:

String word = "APPLE";
int score = letterScore(word) + wordlength_score[word.length() - 1];//acording to you array the length must be [0,12] or else you will get an exception

编辑:你还可以做的是创建一个计算最终得分并返回它的方法,在这种情况下你将不得不做这样的事情:

int scorefinal(String word)
{
    int lettervalue = letterScore(word);//compute the letterscore
    return lettervalue + wordlength_score[word.length() - 1];//or return letterScore(word) + wordlength_score[word.length() - 1];
}