I started my computer science ib 3 weeks ago, and have been asked to create a simple game where a random number is chosen by the program, and the user has 5 chances to guess the number. I've got a problem with the 'sorry, you lost' bit... I need to show a percentage error of how far out the user was. g3 is the number the player guessed on their final chance. Code: else { int pce; if (g3>number) { pce = (((g3-number)/g3)*100); } if (g3<number) { pce = (((number-g3)/number)*100); } System.out.println("No: the number was "+number+". Unlucky, you were only "+pce+"% out!"); } BlueJ highlights the second last line, saying variable pce hasn't been initialised... any ideas why? I can post the rest of the code (108 lines) if it would help Thanks
genius but why do you have to give it a value for it to be initialised? (just so I understand for next time )
It's been a long time since I did any Java, but if I remember correctly, you have to initialise all local primitive data types like that because the compiler doesn't assign them an initial value.
This. Local variables have to be initialised, class variables get default values (typically 0 or null depending if it's a primitive or an object)