我试图用Java创建switch case。毕竟我写了代码我面临这个问题。我想弄清楚,但我没有得到它。
这是我的代码:
import java.io.*;
class question3 {
public static void main (String [] args) throws IOException {
InputStreamReader inStream = new InputStreamReader(System.in);
InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(inStream);
String str;
int choice;
System.out.println("Please Enter Your Number:");
str = stdin.readLine();
choice = Integer.parseInt(str);
switch (choice)
{
case 1:
System.out.println("You selected 1.");
break;
case 2:
case 3:
System.out.println("You selected 2 or 3.");
break;
case 4: System.out.println("You selected 4.");
break;
default:
System.out.println("Select again please.");
}
}
}
CMD出错:
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:{PATH}>javac question3.java
question3.java:6: error: cannot find symbol
InputStreamReader inStream = new InputStreamReader(System.in);
InputStreamReader(System.in);
^
symbol: method InputStreamReader(InputStream)
location: class question3
1 error
答案 0 :(得分:2)
摆脱那一行:InputStreamReader(System.in);
(main
中的第二行)
它对你没有任何帮助。您已使用System.in
进行初始化。
编译器期望变量在那里使用你的语句,因为你正在调用InputStreamReader
构造函数而不对结果对象做任何事情。
答案 1 :(得分:1)
使用扫描仪。
Scanner scanner = new Scanner(System.in);
System.out.println("Please Enter Your Number:");
int choice= scanner.nextInt();