samedi 4 mars 2017

Java If & Else Statement [duplicate]

I was wondering why the following code will not work. It won't change the string for when a user enters if they have a pet or not.

Can anyone explain?

I'm new to Java. Thanks.

import java.util.Scanner;

public class Questions {

public static void main(String[] args){

    //Define Scanner
    //SyppressWarnings, due to Java thinking I'm not using scan var.
    @SuppressWarnings("resource")
    Scanner scan = new Scanner(System.in);

    //Ask User For Name
    System.out.print("Enter Your Name: ");
    String name = scan.nextLine();

    //Ask User For Location
    System.out.print("Which Town Do you Live In: ");
    String location = scan.nextLine();

    //Ask User For Birthday (Day Only)
    System.out.print("Which Day Of The Month Were You Born On: ");
    int bDay = scan.nextInt();

    //Ask User If They Have Any Pets
    System.out.print("Do You Have Any Pets (true/false): ");
    boolean pet = scan.nextBoolean();

    if (pet = true) {
        String pets = "You have some pets!";
        System.out.print("Your name is " + name + " and your live in "
                + location + ", Your day of birth is the " + bDay + ". " + pets);
    }
    else {
        String pets = "You don't have any pets.";
        System.out.print("Your name is " + name + " and your live in "
                + location + ", Your day of birth is the " + bDay + ". " + pets);
    }


}

}

Aucun commentaire:

Enregistrer un commentaire