java - what's wrong with this if else? -


this question has answer here:

i want know wrong :

if (q == "outside" && q1 == "not alive") { system.out.println("bison"); } 

this statement works perfectly:

if (q == "outside" && q1 == "alive") { system.out.println("bison"); } 

all of if statements containing "alive" works perfectly, of if statements containing "not alive" not work.

this exercise have without using else if or else.

full code :-

import java.util.scanner;  class age04 { public static void main(string [] args) {  scanner keyboard = new scanner(system.in);  string q, q1,;  system.out.println("two more questions, baby!"); system.out.println("think of , i'll try guess it!");   system.out.println("question 1) stay inside or outside or both"); q = keyboard.next(); system.out.println("question 1) alive?"); q1 = keyboard.next();   if (q == "inside" && q1 == "alive") { system.out.println("houseplant"); }  if (q == "inside" && q1 == "not alive") { system.out.println("shower curtain"); }  if (q == "outside" && q1 == "alive") { system.out.println("bison"); } if (q == "outside" && q1 == "not alive") { system.out.println("bison"); }  if (q == "both" && q1 == "alive") { system.out.println("dog"); }  if (q == "both" && q1 == "not alive") { system.out.println("cell phone"); }  } } 

use equals() function compare string in java == function not compare string :

          if (q.equals("outside") && q1.equals("not alive"))              {              system.out.println("bison");            } 

try :

    import java.util.scanner;      class age04 {     public static void main(string [] args) {      scanner keyboard = new scanner(system.in);     string q, q1;     system.out.println("two more questions, baby!");    system.out.println("think of , i'll try guess it!");      system.out.println("question 1) stay inside or outside or both");    q = keyboard.next();    system.out.println("question 1) alive?");   q1 = keyboard.next();     if (q.equals("inside") && q1.equals("alive")) {    system.out.println("houseplant");    }   if (q.equals("inside") && q1.equals("not-alive")) {  system.out.println("shower curtain");  }   if (q.equals("outside") && q1.equals("alive")) {  system.out.println("bison"); } if (q.equals("outside") && q1.equals("not-alive")) { system.out.println("bison"); }  if (q.equals("both") && q1.equals("alive")) { system.out.println("dog");  }   if (q.equals("both") && q1.equals("not-alive")) {  system.out.println("cell phone");           }      } } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -