< = opperator工作不工作......给我的错误不知道为什么操作不if语句和扫描仪
Scanner age = new Scanner(System.in);
String age1 = age.nextLine();
System.out.println("How old are you?");
if (age1 <= 20)
System.out.println("Access granted");
}
< = opperator工作不工作......给我的错误不知道为什么操作不if语句和扫描仪
Scanner age = new Scanner(System.in);
String age1 = age.nextLine();
System.out.println("How old are you?");
if (age1 <= 20)
System.out.println("Access granted");
}
什么哟有有一个字符串。你需要解析它。
尝试Integer.parseInt(age1)
。
如果你只想读入数字,你也可以使用nextInt()。 因为你不能确定Input是int,但是在Scanner类中也有一个解决方案,你只需在读出之前使用hasNextInt()。 欲了解更多信息结帐Java API: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
'age1'是字符串。阅读更多关于如何从扫描仪读取的教程。提示_nextInt_ :) – sam