Switch 语句未添加到变量。怎么了?
Switch statement not adding to variable. What is wrong?
我做错了什么?无论输入什么单词(当然是大写字母),程序都会打印 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';
case 1和char值好像不匹配,,先打印char的int值,替换case中相同的值...
public class Switch {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//String word = readLine("Enter your word here: ");
String word ;
Scanner input=new Scanner(System.in);
word = (input.nextLine());
char ch;
int points = 0;
for (int i = 0;i<word.length();i++) {
ch = word.charAt(i);
switch(ch)
{
case 'a': ;
points += 1;
break;
case 'b':
points +=3;
break;
case 'c': ch = 'C';
points +=3;
break;
}
}
System.out.println("Your word gave "+points+" points");
}
我做错了什么?无论输入什么单词(当然是大写字母),程序都会打印 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';
case 1和char值好像不匹配,,先打印char的int值,替换case中相同的值...
public class Switch {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//String word = readLine("Enter your word here: ");
String word ;
Scanner input=new Scanner(System.in);
word = (input.nextLine());
char ch;
int points = 0;
for (int i = 0;i<word.length();i++) {
ch = word.charAt(i);
switch(ch)
{
case 'a': ;
points += 1;
break;
case 'b':
points +=3;
break;
case 'c': ch = 'C';
points +=3;
break;
}
}
System.out.println("Your word gave "+points+" points");
}