0

我正在努力处理这个非常简单的代码:我正在尝试打印出“_”标记,用户输入的单词中的每个字母都有一个 _ 标记。但是,每当我尝试编译代码时,我都会收到“错误:game_3 类中的方法 makeLine 不能应用于给定类型;原因:实际参数列表和形式参数列表的长度不同。”

这似乎是非常明确的反馈,但我认为我并没有真正理解它 - 起初,我认为这是因为我没有为 stringNumber 分配值,但为其分配值并没有帮助。怎么了?

/*Assignment: 
Write a reverse Hangman game in which the user thinks of a word and the computer tries 
to guess the letters in that word. Your program must output what the computer guessed 
on each turn, and show the partially completed word. It also must use pseudorandom
functions to make guesses. That is, it should not simply try all the letters in order, 
nor should it use the user’s input to its advantage. 
*/

import java.util.*;

    public class game_3 {

    public static void main(String[] args) {

            getIntroduction();
            playGame();

    }

    public static void getIntroduction(){
        System.out.println();
        System.out.println();
        System.out.println("*************************");
        System.out.println("Welcome to Hangman");
        System.out.println("In this game, you'll provide a word for the computer to guess.");
        System.out.println();
        System.out.println("The computer will guess letters randomly, and assess whether");
        System.out.println("they can be used to complete your word.");
        System.out.println();
        System.out.println("Let's play!");
        System.out.println();
        System.out.println("*************************");
        System.out.println();

    }

    public static void playGame(){
        Scanner input = new Scanner(System.in);
         System.out.print("Please enter a word: ");
        String hangWord = input.next();
        int stringNum = hangWord.length();

        makeLine();
    }

    public static void makeLine(int stringNum){
    for (int i = 0; i < stringNum; i++){
        System.out.print("_ ");
    }
    }
}
4

1 回答 1

1

方法 makeline 需要一个 int 参数:

public static void makeLine(int stringNum){

您在没有参数的情况下调用它:

makeLine();

你想要的是:

makeLine(stringNum);

编辑:要清楚,这就是正式参数列表(预期)和实际参数列表(你给它的)所指的错误消息。当您提供的方法与预期的不匹配时,发生的另一个常见错误消息是“方法方法名称(预期的参数)不适用于参数(给定的参数)。当类型不匹配时会发生这种情况: 如果你在它需要一个 int 的时候传入一个 String,或者你传入了正确的类型,但是没有顺序。

于 2013-07-16T16:08:35.287 回答