简体   繁体   English

Java'if'和'if else'语句未打印出来

[英]Java 'if' and 'if else' statements not printing out

I am new to Java and have recently started coding within the last week. 我是Java的新手,最近在最近一周内开始进行编码。 I have tried to build some basic things and did the following: 我试图构建一些基本的东西,并做了以下工作:

import java.util.Scanner;

public class App {
    public static void main(String[] args){
        // creating scanner object
        Scanner userSex = new Scanner(System.in);
        System.out.println("Enter your sex (male or female): ");
        String sex = userSex.nextLine(); 
        System.out.println("Thank you, you entered " + sex );

        // new scanner
        Scanner userAge = new Scanner (System.in);
        System.out.println("Are you a child or adult: ");
        String age = userAge.nextLine();
        System.out.println("You are a " + sex + " " + age);

        if (userAge.equals("child")) {
            System.out.println("children");
        } else if (userAge.equals("adult")) {
            System.out.println("adults");
        }
    }
}

Unfortunately however, only the top of the code runs. 但是不幸的是,只有代码的顶部运行。 The below code doesn't run and doesn't print anything out even when I enter "child" or "adult". 下面的代码无法运行,即使我输入“儿童”或“成人”也不会打印任何内容。

if (userAge.equals("child")) {
    System.out.println("children");
} else if (userAge.equals("adult")) {
    System.out.println("adults");
} 

instead of 代替

if(userAge.equals("child")) {
        System.out.println("children");
    }
    else if(userAge.equals("adult")) {
        System.out.println("adults");
    }

do this 做这个

if(age.equals("child")) {
        System.out.println("children");
    }
    else if(age.equals("adult")) {
        System.out.println("adults");
    }

The one thing that was already mentioned is that you only need one scanner object. 已经提到的一件事是您只需要一个扫描仪对象。 It's unnecessary to create one for each string entered. 不必为输入的每个字符串创建一个。 The main problem is that you are testing if userAge equals "child" or "adult", but userAge is the scanner object. 主要问题是您正在测试userAge是否等于“ child”或“ adult”,但是userAge是扫描程序对象。 I think you meant to write age.equals("child"), as age is the actual String entered. 我认为您打算写age.equals(“ child”),因为age是输入的实际String。

The following works: 以下作品:

import java.util.Scanner;

public class App {
public static void main(String[] args){
    //creating scanner object
    Scanner in = new Scanner(System.in);
    System.out.println("Enter your sex (male or female): ");
    String sex = in.next(); 
    System.out.println("Thank you, you entered " + sex );

    //new scanner
    System.out.println("Are you a child or adult: ");
    String age = in.next();
    System.out.println("You are a " + sex + " " + age);

    if(age.equals("child")) {
        System.out.println("children");
    }
    else if(age.equals("adult")) {
        System.out.println("adults");
    }

    }
    }

Rather than creating two separate scanners you just use one and call the next() method. 与其创建两个单独的扫描仪,不如使用一个并调用next()方法。 I also change the condition from if(userAge... to if(age... 我还将条件从if(userAge...更改为if(age...

There is no need to create two scanner object .Just create one scanner object. 无需创建两个扫描仪对象。只需创建一个扫描仪对象。 You can also use BufferedReader instead of Scanner.In your code you use userAge.equals("child") .but this is not right since you store the value returned by scanner in age , use age to compare in if-else codition like age.equals("child") 您也可以使用BufferedReader而不是Scanner。在代码中使用userAge.equals(“ child”)。但这不正确,因为您将扫描器返回的值存储在age中,请使用age来比较if-else条件(如age) .equals(“ child”)

 Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your sex (male or female): ");
        String sex = scanner.nextLine(); 
        System.out.println("Thank you, you entered " + sex );


        System.out.println("Are you a child or adult: ");
        String age = scanner.nextLine();
        System.out.println("You are a " + sex + " " + age);


        if (age.equals("child")) {
            System.out.println("children");
        } else if (age.equals("adult")) {
            System.out.println("adults");
        }

Reference Link: https://docs.oracle.com/javase/tutorial/essential/io/scanning.html 参考链接: https : //docs.oracle.com/javase/tutorial/essential/io/scanning.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM