Reputation: 49
I was getting this error, and I actually did fix it but I want to understand why it works. Here's my code:
class GuessGame{
public void startGame(){
Player p1 = new Player();
Player p2 = new Player();
Player p3 = new Player();
int secretNumber = (int) (Math.random() * 10);
while (true){
//int p1Guess = p1.guess();
//p2Guess = p2.guess();
//p3Guess = p3.guess();
if (p1.guess() == secretNumber){
System.out.println("Player 1 got it right! %d", secretNumber);
break;
} else if (p2.guess() == secretNumber){
System.out.println("Player 2 got it right! %d", secretNumber);
break;
} else if (p3.guess() == secretNumber){
System.out.println("Player 3 got it right! %d", secretNumber);
break;
}
}
}
}
class Player{
int number = 0;
public int guess(){
number = (int) (Math.random() * 10);
System.out.println("I'm guessing %d!", number);
return number;
}
}
class GameLauncher{
public static void main(String[] args){
GuessGame starting = new GuessGame();
starting.startGame();
}
}
So, if I change all the %d
to " stuff " + variable
it stops giving me the error.
All the posts I found online for this error has to do with constructor, that's why I'm asking as I don't need any for this (I shouldn't use any for this exercise)
Upvotes: 1
Views: 69
Reputation: 9177
The println
only takes one or zero argument.
About format string input, use java.io.PrintStream#printf(java.lang.String, java.lang.Object...)
instead.
Upvotes: 2