我的代码有什么问题吗? “if”语句似乎不起作用!我运行该程序并输入我的姓名和年龄。我输入了符合使用该计划的年龄,但它说我太年轻了。我编写了这个,但它不让我使用它!
import java.util.Scanner;
public class learning {
public static void main(String args[]){
Scanner uI = new Scanner(System.in);
System.out.println("Enter your name: ");
System.out.print(uI.nextLine());
System.out.println(", enter your age: ");
uI.nextInt();
int person = 10;
if (person > 10){
System.out.println("You may use this program!");
}else{
System.out.println("You may not use this program. You are too young!");
}
uI.close();
}
}
最佳答案
您没有将 uI.nextInt();
分配给任何 int 变量。喜欢:
System.out.println(", enter your age: ");
int personAge = uI.nextInt();
int person = 10; // instead use this as constant, public static final int MIN_ALLOWED_AGE = 11;
if (personAge > person){ // if (personAge >= MIN_ALLOWED_AGE){
System.out.println("You may use this program!");
}else{
System.out.println("You may not use this program. You are too young!");
}
关于java - 我的用于输入姓名、年龄以及查看一个人是否年龄足以做某事的 Java 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064819/