切换语句不添加到变量。怎么了?

时间:2015-10-26 18:00:52

标签: java for-loop switch-statement

我做错了什么?无论键入什么单词(以大写字母表示),程序都会打印0分。

我认为它与声明变量的位置有关,但我不确定如何解决它。

    public void run() {
    String word = readLine("Enter your word here: ");
    char ch;
    int points = 0;
    for (int i = 0;i<word.length();i++) {
        ch = word.charAt(i);
        switch(ch) {
        case 1: ch = 'A';
        points += 1;
        break;
        case 2: ch = 'B';
        points +=3;
        break;
        case 3: ch = 'C';
        points +=3;
        break;
        case 4: ch = 'D';
        points +=2;
        break;
        case 5: ch = 'E';
        points +=1;
        break;
        case 6: ch = 'F';
        points +=4;
        break;
        case 7: ch = 'G';
        points +=2;
        break;
        case 8: ch = 'H';
        points +=4;
        break;
        case 9: ch = 'I';
        points +=1;
        break;
        case 10: ch = 'J';
        points +=8;
        break;
        case 11: ch = 'K';
        points +=5;
        break;
        case 12: ch = 'L';
        points +=1;
        break;
        case 13: ch = 'M';
        points +=3;
        break;
        case 14: ch = 'N';
        points +=1;
        break;
        case 15: ch = 'O';
        points +=1;
        break;
        case 16: ch = 'P';
        points +=3;
        break;
        case 17: ch = 'Q';
        points +=10;
        break;
        case 18: ch = 'R';
        points +=1;
        break;
        case 19: ch = 'S';
        points +=1;
        break;
        case 20: ch = 'T';
        points +=1;
        break;
        case 21: ch = 'U';
        points +=1;
        break;
        case 22: ch = 'V';
        points +=4;
        break;
        case 23: ch = 'W';
        points +=4;
        break;
        case 24: ch = 'X';
        points +=8;
        break;
        case 25: ch = 'Y';
        points +=4;
        break;
        case 26: ch = 'Z';
        points +=10;
        break;
        }
    }
    println("Your word gave "+points+" points");
}

}

3 个答案:

答案 0 :(得分:5)

你不应该对案件进行编号。做

 case 'A':

而不是

 case 1: ch = 'A';

答案 1 :(得分:1)

案例1和char值似乎不匹配,,,首先打印char的int值并在案例中替换相同的值...

答案 2 :(得分:0)

mysqldump -u <db_username> -p -h <db_host> db_name > database_dump.sql