java - 我的用于输入姓名、年龄以及查看一个人是否年龄足以做某事的 Java 代码有什么问题?

标签 java if-statement

我的代码有什么问题吗? “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/

相关文章:

java - 使用给定公钥的 RSA 加密(Java 中)

java - 构建检查列表中枚举的 if 语句

javascript - 条件练习 addWithSurcharge

C++ 如果没有按预期工作? (真的很奇怪的错误)

java - 设计自定义注释来调用 jar 中的方法

Java VisualVm 使 PID 消失

Windows 文件路径的 Java 正则表达式

java - 如何以编程方式创建 Azure AlertRule

php - 如何在 SQL 查询中动态累积复选框值

java - 如何获取 if 语句中计算的值并在 if 语句之外使用它?