vendredi 19 juin 2015

Boolean and if mistake

I have a problem with this codes: First you enter your name Then you enter your age Then you enter your password (here is "anybody") Name and age will be entered without problem but Boolean make a mistake and when you enter your password correct Boolean show you false answer!! Please Help me!! Thanks.

import java.util.Scanner;

public class MyClass {

public static void main(String[] args) {
    String name;
    int age;
    boolean trueOrFalse;
    boolean trueOrFalse2;

    String builtInPassword = "anybody";

    Scanner keyBoardInput = new Scanner(System.in);

        System.out.print("Please enter your First name: ");
        name = keyBoardInput.next();
        System.out.print("Please enter your age: ");
        age = keyBoardInput.nextInt();
        trueOrFalse = false;
        trueOrFalse2 = true;
        System.out.print("Please enter your Password: ");
    if (keyBoardInput.next() == builtInPassword) {
        System.out.println(trueOrFalse2);
    } else {
        System.out.println(trueOrFalse);
    } 

}

}

Aucun commentaire:

Enregistrer un commentaire