实际和形式参数列表的长度不同> "%d",变量

时间:2017-08-11 16:49:48

标签: java

我收到了这个错误,我确实修复了它,但我想了解它为什么会起作用。 这是我的代码:

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,它就会停止向我提供错误 我在网上找到的这个错误的所有帖子都与构造函数有关,这就是为什么我要问,因为我不需要任何这个(我不应该使用任何这个练习) )

2 个答案:

答案 0 :(得分:2)

println只接受一个或零参数。

enter image description here

关于格式字符串输入,请改用java.io.PrintStream#printf(java.lang.String, java.lang.Object...)

答案 1 :(得分:0)

使用方法System.out.printf(" Some Test%s"," String");