Probleem gedeeltelijk nu opgelost alleen zorgt de oplossing voor een nieuw probleem. 3 personen mogen raden alleen blijft hun invoer op 0 staan en neemt hij niet de waarde over wat ik in geef.
Ik denk dat ik het anders moet aanpakken en voor alle 3 een aparte invoer moet maken??
In eerste instante was de code voor het raden:
Code:
number = (int) (Math.random() * 10);
Ik heb de volgende 3 classes:
Code:
package guessingGame;
public class GuessGame {
Player p1;
Player p2;
Player p3;
public void startGame() {
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 0;
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 guessed " + guessp1);
guessp2 = p2.number;
System.out.println("player two guessed " + guessp2);
guessp3 = p3.number;
System.out.println("player three guessed " + guessp3);
if (guessp1 == targetNumber) {
p1isRight = true;
}
if (guessp2 == targetNumber) {
p1isRight = true;
}
if (guessp3 == targetNumber) {
p1isRight = 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; // stopt de loop
} else {
System.out.println("Players will have to try again.");
} // eind if/else
} // einde while
} // einde method
} // einde class
Code:
package guessingGame;
import javax.swing.JOptionPane;
public class Player {
int number = 0;
String input = "";
public void guess() {
input = JOptionPane.showInputDialog(null, "Guess the number", "Number Guess", JOptionPane.QUESTION_MESSAGE);
int number = Integer.parseInt(input);
System.out.println("I'm guessing " + number);
}
}
Code:
package guessingGame;
public class GameLauncher {
public static void main (String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}