+ 2

FIX ERROR in my code ,project guesser game ,code in description

this is a guessergame project, there are 5 objects , umpire ,guesser , player1 , player 2 and player3 . the umpire begins the game ,and the guesser is supposed to guess a number , the umpire collects it , then player 1 is suppose to guess the another number , and player 2 and player 3 ,, and umpire collects from all , if the number of any player matches with number of guesser , we have to print that , that individual player won the game else try again ,,,,,,,,, MY CODE IS RUNNING BUT WHY EVERYTIME PLAYER 1 IS WINNING ?? PLEASE FIX IT code below import java.util.*; class guessGame { int num,num1,num2,num3,number; public int guessNum(){ Scanner sc = new Scanner(System.in); int number = sc.nextInt(); return number; } public static void main (String args[]){ guessGame umpire = new guessGame(); guessGame guesser = new guessGame(); guessGame player1= new guessGame(); guessGame player2= new guessGame(); guessGame player3= new guessGame(); int num =guesser.guessNum(); int num1 =player1.guessNum(); int num2 = player2.guessNum(); int num3= player3.guessNum(); umpire.compare(); } public void compare(){ if(num==num1){ System.out.println("player 1 won the game "); }else if(num==num2){ System.out.println("player 2 won the game"); }else if (num==num3){ System.out.println("player 3 won the game "); } else{ System.out.println("game drawn "); } } }

27th Mar 2023, 6:55 AM
Shubham Swaraj 161
Shubham Swaraj 161 - avatar
6 Answers
+ 4
Make variables num, number, num1, num2, num3 and Scanner object static. Then it works.. You are redeclaring these in main again. Don't do it..
27th Mar 2023, 9:42 AM
Jayakrishna 🇼🇳
+ 4
My normal way to identify and find problems is to put print statements at the different stages of the code. Check your num, num1, num2 etc (it's meant to be user input, so you should know what the values are, but what does the system do?) Check the values that are returned. Check the values in your compare method.
27th Mar 2023, 7:03 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
https://www.sololearn.com/discuss/2232645/?ref=app https://www.sololearn.com/discuss/2101575/?ref=app https://www.sololearn.com/discuss/2936774/?ref=app https://www.sololearn.com/discuss/3065920/?ref=app declaring variable static means "it belongs to a class, not to object. all objects are will point to same value and it is accessed by the class name. edit: in c, it retains it's value between different function calls. in java, variable belong to class, not individual objects. "className.variable" or "object.variable" returns same value while for normal variable "object.variable" will be different for different objects.. here className.variable will return error as non-static variable cannot referenced from static content. (on general context)
28th Mar 2023, 6:54 AM
Jayakrishna 🇼🇳
+ 1
Jayakrishna 🇼🇳 thank you so much it worked!! What it means by declaring a variable static?
27th Mar 2023, 10:17 AM
Shubham Swaraj 161
Shubham Swaraj 161 - avatar
+ 1
@Ausgrindtube yeah i tried that way, got some clearity
27th Mar 2023, 10:17 AM
Shubham Swaraj 161
Shubham Swaraj 161 - avatar
+ 1
Happy that it could help!
27th Mar 2023, 10:59 AM
Ausgrindtube
Ausgrindtube - avatar