I need help with this console game | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I need help with this console game

I'm trying to make a console game in java that tells what your number is It keeps saying your number is 0 at the end and it is not setting variables to any value other than 0 Here it is: import java.util.Scanner; class Program { public static void main(String[] args) { int a; int b; int c; int d; int e; int number; String answer; System.out.println("Pick a number from 1-31\n"); System.out.println("Is your number one of these? 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31"); Scanner a1 = new Scanner(System.in); answer = a1.next(); if(answer == "yes") { a = 1; } else { a = 0; } System.out.println("Is your number one of these? 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31"); Scanner a2 = new Scanner(System.in); answer = a2.next(); if(answer == "yes") { b = 2; } else { b = 0; } System.out.println("Is your number one of these? 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31"); Scanner a3 = new Scanner(System.in); answer = a3.next(); if(answer == "yes") { c = 4; } else { c = 0; } System.out.println("Is your number one of these? 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31"); Scanner a4 = new Scanner(System.in); answer = a4.next(); if(answer == "yes") { d = 8; } else { d = 0; } System.out.println("Is your number one of these? 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31"); Scanner a5 = new Scanner(System.in); answer = a5.next(); if(answer == "yes") { e = 16; } else { e = 0; } number = a + b + c + d + e; System.out.println("Your number is " + (a + b + c + d + e)); } }

8th Oct 2017, 1:49 PM
Joshua
Joshua - avatar
2 Answers
+ 3
You made a very common mistake in your code. Never ever compare java strings with "=="!!!! this is the comparison for the same reference but not the value of the string! Use "equals" (eg. "blablabla".equals("blablabla");) this is the correct way of comparing strings. I used to make this error a lot when i started java so you are not alone ;)
8th Oct 2017, 1:58 PM
Chrizzhigh
Chrizzhigh - avatar
+ 2
Thanks man
8th Oct 2017, 2:00 PM
Joshua
Joshua - avatar