如果声明评估

时间:2013-11-08 15:47:13

标签: java

使用下面的代码即使我输入的数字大于18,我也会得到这个结果 跑: 你几岁? 21 你尚未达到多数人的年龄! 建立成功(总时间:3秒)

我是java的新手,尝试自学,任何人都可以帮忙吗?

import java.util.Scanner;
public class Chapter8 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner reader = new Scanner (System.in);
        // TODO code application logic here

        //Excercise 15
        System.out.print("How old are you? ");
        int x = Integer.parseInt(reader.nextLine());
        if (x > 18){
            System.out.println("You have not reached the age of Majority yet!");
        }else {
            System.out.println("You have reached the age of Majority!");
        }

3 个答案:

答案 0 :(得分:5)

您的代码目前显示“如果年龄大于18岁(即19岁或以上),那么他们不是年龄”。

我认为你的意思是x < 18

答案 1 :(得分:0)

应该是相反的方式:

if (x < 18){
       System.out.println("You have not reached the age of Majority yet!");
}else {
        System.out.println("You have reached the age of Majority!");
}

答案 2 :(得分:0)

你做了一个声明,如果x&gt;只打印一行。 18.你应该用else if语句来完成陈述。

public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    // TODO code application logic here

    //Excercise 15
    System.out.print("How old are you? ");
    int x = Integer.parseInt(reader.nextLine());
    if (x > 18){
        System.out.println("You have not reached the age of Majority yet!");
    }
    else if (x <= 17){
        System.out.println("You have reached the age of Majority!");
    }

}