我正在尝试使用基于字符串的游戏板在BlueJ中编写hangman代码。我将游戏板作为字符串放在不同的类中,并尝试在我的主类中调用它,以便在输入所需的猜测单词后打印出来。每次我运行代码时,它都会打印带有随机数字和字母的游戏板@,而不是实际的绞刑者游戏板。在上面的代码中,我将void main放入Hangman类中,然后将游戏板的代码放入游戏板类中。
Hangman类:
`public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Player 1, enter a word! ");
word = in.nextLine();
wordTwo = word;
System.out.println("Your word is " + wordTwo);
System.out.println("Press (p) when you're ready to continue!");
start = in.nextLine();
wordChars = wordTwo.toCharArray();
if (start.equals("p"))
{
CLS();
}
else
{
System.out.println("Wait what? Restart the program and try typing
the key that ACTUALLY STARTS THE GAME!");
}
Gameboard board = new Gameboard(); //Line of code that's supposed to
call the game board.
System.out.println(board); //Line of code to print game board.
while ( trap == 1)
{
System.out.println("Player 2, will you guess a word(w) or a
character(c)?");
System.out.println("Type 'e' to exit");
wordOrChar = in.nextLine();
if (wordOrChar.equals("w"))
{
guessWord(guessWord);
}
else if(wordOrChar.equals("c"))
{
guessChar(guessChar);
}
exit = in.nextLine();
if (exit.equals("e"))
{
trap = 2;
}
}
}`
游戏板类:
public String board(String board)
{
board = (" -------------" + "\n" + "| |" + "\n" + " |" + "\n" + " |" + "\n" + " |" + "\n" + " |" + "\n" + " |" + "\n" + " |" + "\n" + " |" + "\n" + "---------------");
return board;
}
发布于 2018-06-10 02:13:42
在你的游戏板类中,它需要一个传递给它的参数(字符串板),但是当它被调用时,却没有参数被传递。删除所需的参数,一切都将正常。如果失败,请将类变量重命名为gameBoard并返回它,而不是board。:)
https://stackoverflow.com/questions/50261964
复制相似问题