samedi 22 août 2015

Java nested if statement not printing anything or giving error [duplicate]

This question already has an answer here:

I have been trying to figure this out for hours. Why isn't java picking up the statements in my if statements at the bottom. Says use nested if statements. Please help. Thx This is what I should see:

TWO QUESTIONS! Think of an object, and I'll try to guess it.

Question 1) Is it animal, vegetable, or mineral?

animal

Question 2) Is it bigger than a breadbox?

no

My guess is that you are thinking of a mouse. I would ask you if I'm right, but I don't actually care.

import java.util.Scanner;

public class App1 {

    public static void main(String[] Args) {

        String q1;
        String q2;

        String a = "animal";
        String b = "vegetbale";
        String c = "mineral";
        String d = "yes";
        String e = "no";


        Scanner keyboard = new Scanner(System.in);

        System.out.println("TWO QUESTIONS!");
        System.out.println("Think of an object and I'll try to guess it.");
        System.out.println();
        System.out.println("Question1) Is it animal, vegetable or mineral?");
        System.out.print(">");
        q1 = keyboard.nextLine();
        System.out.println();

        System.out.println("Question2) Is it bigger than a breadbox?");
        System.out.print(">");
        q2 = keyboard.nextLine();
        System.out.println();


        if (q1 == a && q2 == d) {
            System.out.println("My guess is that you are thinking of a squirrel");
            System.out.println("I would ask if you if I'm right, but I don't actually care");

        } if (q1 == a && q2 == e) {
            System.out.println("My guess is that you are thinking of a moose");
            System.out.println("I would ask if you if I'm right, but I don't actually care");
        }  if (q1 == b && q2 ==d) {
            System.out.println("My guess is that you are thinking of a carrot");
            System.out.println("I would ask if you if I'm right, but I don't actually care");
        }  if (q1 == b && q2 ==e) {
            System.out.println("My guess is that you are thinking of a watermelon");
            System.out.println("I would ask if you if I'm right, but I don't actually care");
        }  if (q1 == c && q2 ==d) {
            System.out.println("My guess is that you are thinking of a paperclip");
            System.out.println("I would ask if you if I'm right, but I don't actually care");
        } if (q1 == c && q2 ==e) {
            System.out.println("My guess is that you are thinking of a camaro");
            System.out.println("I would ask if you if I'm right, but I don't actually care");
        }

    }

}

Aucun commentaire:

Enregistrer un commentaire