This question already has an answer here:
- How do I compare strings in Java? 23 answers
I'm generating a random number from 1 to 100 and showing it to user and then I will get 20 chances to to guess that random number. The response should be either YES or NO but right now when I run this program it just takes input from user and end the execution it is not visiting the if else if loop
Code
public static void main(String args[])
{
Random rand = new Random();
int rand_int1 = rand.nextInt(100);
System.out.println("Random value is = " + rand_int1);
int n = 20;
Scanner userinput = new Scanner(System.in);
System.out.println("is your number is form 0 to 50 ?");
String ans1 = userinput.nextLine();
System.out.println("ans : " + ans1);
if (ans1 == "yes") {
System.out.println("number is more then 50");
}
else if (ans1 == "no") {
System.out.println("number is less then 50");
}
}
Example Output
Random value : 23
is your number is more then or equal to 50?
ans : No
Later, I will add more code so this will also be in the output:
is your number is less then or equal to 25?
ans : Yes
Aucun commentaire:
Enregistrer un commentaire