我收到了这个错误,我确实修复了它,但我想了解它为什么会起作用。 这是我的代码:
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();
}
}
因此,如果我将所有%d
更改为" stuff " + variable
,它就会停止向我提供错误
我在网上找到的这个错误的所有帖子都与构造函数有关,这就是为什么我要问,因为我不需要任何这个(我不应该使用任何这个练习) )
答案 0 :(得分:2)
答案 1 :(得分:0)
使用方法System.out.printf(" Some Test%s"," String");