简体   繁体   中英

Java 'if' and 'if else' statements not printing out

I am new to Java and have recently started coding within the last week. 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. I think you meant to write age.equals("child"), as age is the actual String entered.

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. I also change the condition from if(userAge... to 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")

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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