首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >If语句求值

If语句求值
EN

Stack Overflow用户
提问于 2013-11-08 23:47:13
回答 3查看 75关注 0票数 0

使用下面的代码,即使我输入了一个大于18的数字,我也会得到这个结果。

你多大了? 21岁还没到法定年龄呢!构建成功(总时间:3秒)

我是java的新手,正在尝试自学,有人能帮我吗?

代码语言:javascript
复制
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!");
        }
EN

回答 3

Stack Overflow用户

发布于 2013-11-08 23:49:02

你的代码当前是这样的:“如果年龄大于18岁(即19岁或更高),那么他们就不是年龄”。

我想你指的是x < 18

票数 5
EN

Stack Overflow用户

发布于 2013-11-08 23:51:39

应该是另一种方式:

代码语言:javascript
复制
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!");
}
票数 0
EN

Stack Overflow用户

发布于 2013-11-08 23:53:28

您创建了一个语句,该语句仅在x> 18时才会打印一行。您应该使用else if语句来结束该语句。

代码语言:javascript
复制
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!");
    }

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19863069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档