我能';我的hangman java程序似乎无法运行
我正在尝试制作一个hangman程序,我没有错误,但是当我运行它时程序将终止。你看到我的代码有什么问题吗?我可以向代码中更改的字符串添加StringBuilder吗? 我还想创建一个驱动程序类,但不确定要在其中放入什么我能';我的hangman java程序似乎无法运行,java,Java,我正在尝试制作一个hangman程序,我没有错误,但是当我运行它时程序将终止。你看到我的代码有什么问题吗?我可以向代码中更改的字符串添加StringBuilder吗? 我还想创建一个驱动程序类,但不确定要在其中放入什么 package hangman; import java.util.Random; public class hangman { public static void main1(String[] args){ } //the word to be gu
package hangman;
import java.util.Random;
public class hangman
{
public static void main1(String[] args){
}
//the word to be guessed
private String word = null;
// the array of possible words */
private String[] wordArray = {"help","word","work","pant", "farm", "blue", "swim", "bike", "jump", "snow"};
// the random number generator */
private Random randNumGen = new Random();
// the characters that were guessed that were wrong */
private String wrongGuesses = "";
// the characters that were guessed that were right */
private char[] rightGuesses = {' ',' ',' ', ' '};
// Randomly picks the word from the wordArray
public hangman()
{
int index = randNumGen.nextInt(wordArray.length);
word = wordArray[index];
}
public boolean guess()
{
boolean done = false;
Object SimpleInput;
// get input from user
String guessStr = ("Enter a letter");
// check if still have at least one letter
if (guessStr.length() > 0)
{
// get first letter
char guessChar = guessStr.charAt(0);
// check this letter
done = this.guess(guessChar);
}
return done;
}
//Method to guess a letter
public boolean guess(char guessChar)
{
int index = word.indexOf(guessChar);
boolean done = false;
// if the letter is in the word
if (index >= 0)
{
// add letter to correctly guessed letters
rightGuesses[index] = guessChar;
// check if the user won
int numRightGuesses = 0;
if (numRightGuesses == 4)
{
done = true;
}
}
else
{
// add letter to string with wrong letters
wrongGuesses = wrongGuesses + guessChar + " ";
int numWrongGuesses = 0;
// check if this was the last wrong guess
if (numWrongGuesses == 6)
{
done = true;
}
}
return done;
}
/**
* Method to play the game till the user
* wins or loses
*/
public void playGame()
{
boolean done = false;
// loop while we haven't reached the end of the game
while (!(done = guess()))
{}
}
}
main()方法为空。这是进入你的应用程序的主要入口点,它什么也不做,所以程序会干净地退出。成功…?用以下代码替换主方法,它将创建类的对象,并执行构造函数中的代码
public static void main(String[] args){
hangman game = new hangman();
game.PlayGame();
}
你的节目需要一个起点。 计算机需要知道你的程序从哪里开始 您的
publicstaticvoidmain(String[]args)
方法开头的“main”后面有一个尾随“1”,这是一个点。
另一点是,您必须使用main方法调用您的游戏
public static void main(String[] args) {
hangman game = new hangman();
game.playGame();
}
首先,main方法需要命名为main,而不是main1。第二,这是你的应用程序运行时被调用的内容,所以你所有的启动逻辑都应该放在那里:)你应该从练习“hello world”程序开始。在我将主要方法更改为上述方法后,还有什么其他原因不能运行吗?