Ale*_*207 0 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");
}
}
Run Code Online (Sandbox Code Playgroud)
你不应该对案件进行编号.做
case 'A':
Run Code Online (Sandbox Code Playgroud)
代替
case 1: ch = 'A';
Run Code Online (Sandbox Code Playgroud)