我试图将一个字符串添加到方法但返回错误,我错过了什么?

时间:2016-07-02 12:43:46

标签: java

我对编程很新。 。

我收到以下代码的错误:

public class Main {

public static void main(String[] args) {

    int highScore = calculateScore(true, 800, 5, 100);
    System.out.println("Your final score was " + highScore);


    highScore = calculateScore(true, 10000, 8, 200);
    System.out.println("Your final score was " + highScore);

    displayHighScore(Jack, 3);
}

public static int calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {
    if (gameOver) {
        int finalScore = score + (levelCompleted * bonus);
        finalScore += 2000;
        return finalScore;
    }
    return -1;
}

public static void displayHighScore(String playersName, int position){
    System.out.println(playersName + " managed to get to position " + position);
   }  }

2 个答案:

答案 0 :(得分:1)

我们没有足够的信息但是 检查错误消息

  

错误:(14,26)java:不兼容的类型:int无法转换为   java.lang.String中

看起来问题是关于你如何调用你所写的方法......

您传递的是一个必须传递字符串的整数...

查看签名并准确提供该方法需要的参数...

Stringint

public static void main(String[] args) {
    displayHighScore(2, 1);  // for example this is a wrong paramerer error
    displayHighScore("C. Bryan", 1);  // this is ok!

}

public static void displayHighScore(String playersName, int position) {
    System.out.println(playersName + " managed to get to position " + position);

}

答案 1 :(得分:-8)

尝试更改为:

System.out.println(playersName + " managed to get to position " + position.toString());

Java没有合并到字符串 - 你必须自己做。