java - Why isn't my code working beyond printing the math.random? -
this question has answer here:
- how compare strings in java? 23 answers
i'm new programmer , trying teach myself java doing random projects. below "rock, paper, scissors" game , issue i'm facing after printing "a", program ends , not continue onto if else statements below. can given appreciated.
package com.company; import java.util.scanner; public class main { public static void main(string[] args) { system.out.println("hello & welcome rock, paper, scissors. what's name?"); scanner scan = new scanner(system.in); string userchoice = scan.nextline(); system.out.println("hello, " + userchoice + ". let's start game!"); scanner scan = new scanner(system.in); system.out.println("choose one: rock, paper, scissors"); string userfirstchoice = scan.nextline(); system.out.println("you chose " + userfirstchoice); double = math.random(); system.out.println(a); if (a >= 0.00 && a<= 0.3){ if ( userfirstchoice== "rock"){ system.out.println("rock vs rock: tie"); } else if (userfirstchoice == "paper"){ system.out.println("rock vs paper: lose!"); } else if (userfirstchoice == "scissors"){ system.out.println("rock vs scissors: win!"); } } else if (a>=0.3 && a<=0.6){ if(userfirstchoice == "paper"){ system.out.println("paper vs paper: tie!"); } else if (userfirstchoice == "rock"){ system.out.println("rock vs paper: lose!"); } else if(userfirstchoice == "scissors"){ system.out.println("scissors vs paper: win!"); } } else if (userfirstchoice == "scissors"){ system.out.println("scissors vs scissors: tie!"); } else if (userfirstchoice == "paper"){ system.out.println("paper vs scissors: lose!"); } else if (userfirstchoice == "rock") { system.out.println("rock vs scissors: win!"); } } }
you can't use ==
compare strings in java. (==
compares references, , operands ==
references different strings though string contents identical.)
use userfirstchoice.equals("scissors")
etc. instead.
your use of relational operators on double
types is correct.
Comments
Post a Comment