import java.util.Scanner;
class G2Ex3 {
public static void main (String args[]) {
Scanner in = new Scanner (System.in);
System.out.print("Enter password: ");
String pw = in.nextLine();
switch (pw) {
case "JRU":
case "jru":
System.out.println("Password Accepted!");
break;
default:
System.out.println("Invalid Password!");
}
}
我的代碼,你可以看到有無關的整數。我的問題是爲什麼程序不斷告訴我它只發現java.lang.String,它需要int?謝謝。
什麼版本的Java?在Java 7之前,您無法使用字符串開關。 –
[相關](http://stackoverflow.com/a/338230/335858)。 – dasblinkenlight
在密碼的情況下,我真的推薦使用char。它比字符串好一點,你也可以使用switch-case。 – iWumbo