lundi 3 juillet 2017

If statement does not work java [duplicate]

This question already has an answer here:

I'm new in java and I've been trying that my code work, but I have not succeeded, the first if statement does not work :

public String get(){
    Scanner reader = new Scanner(System.in);
    System.out.println("Do you have your number? answer Yes to start the game: ");
    String g = reader.nextLine();
    return g; 
}
public void game(){
    String resp = this.get();
    ArrayList<Integer> abc = new ArrayList<Integer>();
    Scanner reader = new Scanner(System.in);
    Random r = new Random();
    int gesses = 1;
    int menor = 1;
    int mayor = 100;
    if (resp == "yes"){
        while(true){
            int num = r.nextInt(mayor - menor + 1 ) + menor;
            if (abc.indexOf(num) > -1) {
                num = r.nextInt(mayor - menor + 1 ) + menor;
            }else {
                abc.add(num);
            }
            System.out.println("Is this your number?: "+num+" ");
            String g = reader.nextLine();
            if (g == "no") {
                System.out.println("Too low or too high?:");
                String n = reader.nextLine();
                if ((n != "low") || (n != "high")) {
                    System.out.println("Did you type correctly? try again:");
                    n = reader.nextLine();
                    if (n == "low") {
                        menor = num;
                    }else if (n == "high") {
                        mayor = num;
                    }
                }
            }else {
                System.out.println("Good for me and it just took me "+gesses+" gesses");
                break;
            }
            gesses+=1;
        }
    }

}

when I run the program the first question that is in method get shows, but then when I type "yes" the first if validates if the answer is equal to yes and in that part my program stops, I do not know why

Aucun commentaire:

Enregistrer un commentaire