gpt4 book ai didi

java - Switch 语句未添加到变量。怎么了?

转载 作者:行者123 更新时间:2023-12-01 07:53:37 32 4
gpt4 key购买 nike

我做错了什么?无论输入什么单词(当然是大写字母),程序都会打印 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");
}

}

最佳答案

您不应该对案例进行编号。做

 case 'A':

而不是

 case 1: ch = 'A';

关于java - Switch 语句未添加到变量。怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33352561/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com