Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: Variable holder and its value?
    text
    copied!<p>I'm having a tough time understanding something from one of my exercises in a book(Complete beginner).</p> <p>The example code was to make a guessing game with three players and it generates a random int for all three players which has to come out the same as the randomly generated int by the game.</p> <p>The code contains three classes, but this is the most important one where I have a question. The other two are just GameLauncher class and Player(The one that plays against the other three players) class.</p> <pre><code>public class GuessGame { Player p1; Player p2; Player p3; public void startGame(){ p1 = new Player(); p2 = new Player(); p3 = new Player(); int guessp1 = 10; int guessp2 = 0; int guessp3 = 0; boolean p1isRight = false; boolean p2isRight = false; boolean p3isRight = false; int targetNumber = (int) (Math.random() * 10); System.out.println("I'm thinking of a number between 0 and 9..."); while(true){ System.out.println("Number to guess is "+targetNumber); p1.guess(); p2.guess(); p3.guess(); guessp1 = p1.number; System.out.println("Player one has guessed " + guessp1); guessp2 = p2.number; System.out.println("Player two has guessed " + guessp2); guessp3 = p3.number; System.out.println("Player three has guessed " + guessp3); if (guessp1 == targetNumber) { p1isRight = true; }if (guessp2 == targetNumber){ p2isRight=true; }if (guessp3 == targetNumber){ p3isRight=true; } if (p1isRight||p2isRight||p3isRight){ System.out.println("We have a winner!"); System.out.println("Player one got it right? " + p1isRight); System.out.println("Player two got it right?" + p2isRight); System.out.println("Player three got it right? " + p3isRight); System.out.println("The game is over."); break; }else{ System.out.println("None of you got it right! Try again!"); } } } } </code></pre> <p>From the above code:</p> <pre><code> int guessp1 = 10; int guessp2 = 0; int guessp3 = 0; </code></pre> <p>is the one that I don't understand. Originally, all of them are assigned the value 0. I tried to assign 10 to see what would happen, but nothing changed. The game played out just the same.</p> <p>My question is, what is the significance for the value assigned to a declared integer if any at all? Especially in this situation.</p> <p>Excluding normal uses like say just printing it or manipulating it with math. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload