0

I'm trying to write a program to calculate the score of a word, based on the game Scrabbleenter image description here

The scores are based off the image above.

I've currently coded a function, my ideal goal is to use this and get the user to input a word to calculate the score.

int scrabbleScore(String Word) {
        int score = 0;
        for (int i = 0; i < Word.length(); i++){
            char calculatedLetter = Word.at(i);
            switch (calculatedLetter) {
                case 'A':
                case 'E':
                case 'I':
                case 'L':
                case 'N':
                case 'O':
                case 'R':
                case 'S':
                case 'T':
                case 'U':
                    score +=1; break;
                case 'D':
                case 'G':
                    score +=2; break;
                case 'B':
                case 'C':
                case 'M':
                case 'P':
                    score +=3; break;
                case 'F':
                case 'H':
                case 'V':
                case 'W':
                case 'Y':
                    score +=4; break;
                case 'K':
                    score +=5; break;
                case 'J':
                case 'X':
                    score +=8; break;
                case 'Q':
                case 'Z':
                    score +=10; break;
                default: break;
            }
        }
        return score;

Why is this giving me a score of 0 for any word?

4

3 回答 3

1

您可以将其缩短一点,并为有一天的多语言做准备,只需进行少量修改。

int scrabbleScore(string Word)
{
    int score = 0;
    char EnglishScoreTable[26] = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 };
    for (auto Letter : Word)
    {
        if (Letter >= 'A' && Letter <= 'Z')
        {
            score += EnglishScoreTable[Letter - 'A'];
        }
        else
        {
            // error in input 
        }
    }
    return score;
}
于 2013-09-24T14:36:35.037 回答
0
std::string word;
std::cin>>word;
std::cout<<"Your Score :" << scrabbleScore(word) ;

还,

int scrabbleScore(String Word)
                  ^ this should be string from std namespace,
于 2013-09-24T14:10:30.540 回答
0

将 imp 更改为:

private static int scrabbleScore(String Word) {
    int score = 0;
    String upperWord = Word.toUpperCase();
    for (int i = 0; i < upperWord.length(); i++){
        char calculatedLetter = upperWord.charAt(i);
        switch (calculatedLetter) {
于 2013-09-24T14:38:53.950 回答