即便是你要显示加载,也应该用import java.lang.*; eclipse这样解释:Only a type can be imported, java.lang resolves to a package
再补充下: 你所定义的a是char型,不能直接用来与数字相比。要比也应该这样 a == '1'。 变量a是需要初始化的 比如 char a = '\000'; -1已经是2个字符了,而char类型的a 只能是一个字符,所以判断是否有输入也不能这样做。 我修改了你的程序如下: import java.io.*;public class Response { public static void main(String[] args) { char a = '\000'; \\000 表示 null System.out.println("choose a number 1-3:"); try{ a=(char)System.in.read(); } catch(IOException e) { System.out.println(e.toString()); } if (a == '\r') \\ \r 表示 用户输入了一个回车符 System.out.println("\nNo character entered."); else if (a == '1') System.out.println("\nYou have entered a 1."); else if (a == '2') System.out.println("\nYou have entered a 2."); else if (a == '3') System.out.println("\nYou have entered a 3."); else System.out.println("\nYou have entered a wrong number!"); } }
java.lang包不用加载,自动加载的
加一句
try{
a=(char)System.in.read();
}catch(Exception e)
{e.e.printStackTrace();}
a你也没有初始化
好象是你的环境变量没有设对!
eclipse这样解释:Only a type can be imported, java.lang resolves to a package
你所定义的a是char型,不能直接用来与数字相比。要比也应该这样 a == '1'。
变量a是需要初始化的 比如 char a = '\000';
-1已经是2个字符了,而char类型的a 只能是一个字符,所以判断是否有输入也不能这样做。
我修改了你的程序如下:
import java.io.*;public class Response { public static void main(String[] args)
{
char a = '\000'; \\000 表示 null
System.out.println("choose a number 1-3:");
try{
a=(char)System.in.read();
} catch(IOException e) {
System.out.println(e.toString());
}
if (a == '\r') \\ \r 表示 用户输入了一个回车符
System.out.println("\nNo character entered.");
else if (a == '1')
System.out.println("\nYou have entered a 1.");
else if (a == '2')
System.out.println("\nYou have entered a 2.");
else if (a == '3')
System.out.println("\nYou have entered a 3.");
else System.out.println("\nYou have entered a wrong number!"); }
}