所以我刚完成了这个项目,我正在为了好玩而做。我对GUI有一个基本的了解,但并不是全部,并且实际上没有把它放到我的一个项目中。我不确定如何在我的代码中实现这一切,但人们可以给我建议或者至少指出我正确的方向吗?
游戏是HangMan。
代码: 公共课HangMan {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String mysteryGuess = "hello";
String userGuess = "";
int wrongGuesses = 0;
boolean[] guessed = new boolean[mysteryGuess.length()];
System.out.println("Hello and welcome to Hang Man!");
loop:
for(;;){
String[] wordAsArray = convertToStringArray(mysteryGuess);
for (int i = 0; i<wordAsArray.length;i++)
if(wordAsArray[i].equals(userGuess))
guessed[i]=true;
System.out.println("Word so far:" + visibleWord(wordAsArray,guessed));
System.out.println("What is your guess?");
userGuess = sc.next();
boolean guessResult = guess(userGuess,wordAsArray,guessed);
if (guessResult==(true))
System.out.println("Correct");
else{
System.out.println("Incorrect");
wrongGuesses++;
}
if (didWin(guessed)==true)
break loop;
}
System.out.println("Good Job! The word was " + mysteryGuess);
System.out.println("You only got " + wrongGuesses + " wrong!");
}
//This method creates an array version of the parameter word
//For example, if word contained the data "hello", then this method
//would return {"h", "e", "l", "l", "o"}
//Parameters: word - a single word
//Returns: an array containing each letter in word
public static String[] convertToStringArray(String word) {
String [] pWord = new String [word.length()];
for (int i = 0; i<pWord.length; i++){
pWord[i] = word.substring(i,i+1);
}
return pWord;
}
//This method determines whether the player has won the game of HangMan
//Parameters: guessed - array of boolean values
//Returns: true - if every value in guessed is true
// false - if at least one value in guessed is false
public static boolean didWin(boolean[] guessed) {
boolean bGuess = true;
loop:
for (int i = 0; i<guessed.length;i++){
if(guessed[i]==false){
bGuess = false;
break loop;
}
}
return bGuess;
}
//This method determines what portion of the hidden word is visible
//For example, if the parameters are as follows:
// wordAsArray: {"h", "e", "l", "l", "o"}
// guessed: {true, false, false, false, true}
//Then the method should return "h???o"
//Parameters: wordAsArray - the individual letters to be guessed
// guessed - array of boolean values; a true value means the corresponding letter has been guessed
//Returns: A string representing how much of the word has been guessed (unguessed letters are represented by ?'s)
public static String visibleWord(String[] wordAsArray, boolean[] guessed) {
String visibleWord="";
String[] holder = new String [wordAsArray.length];
for (int i = 0; i<holder.length;i++)
holder[i]=wordAsArray[i];
for(int i = 0; i<holder.length;i++){
if (guessed[i] == true)
holder[i]=holder[i];
if (guessed[i] == false)
holder[i]="?";
}
for(int i = 0; i<holder.length;i++){
visibleWord=visibleWord+holder[i];
}
return visibleWord;
}
//This method checks to see if a player has made a successful guess in the game of Hang Man
//For example, if the parameters are as follows:
// letter: "e"
// wordAsArray: {"h", "e", "l", "l", "o"}
// guessed: {true, false, false, false, true}
//Then the guessed array would be changed to:
// guessed: {true, true, false, false, true}
//And the method would return false
//Parameters: letter - the letter that the user has just guessed
// wordAsArray - an array of individual letters that are to be guessed
// guessed - array of boolean values; a true value means the corresponding letter has been guessed
//Returns: true - if letter matches an unguessed letter in wordAsArray
// false - otherwise
public static boolean guess(String letter, String[] wordAsArray, boolean[] guessed) {
boolean appearsAtLeastOnce=false;
for(int i = 0; i<wordAsArray.length;i++)
if(letter.equalsIgnoreCase(wordAsArray[i])){
guessed[i] = true;
appearsAtLeastOnce=true;
}
return appearsAtLeastOnce;
}
谢谢你的时间!
答案 0 :(得分:1)
如果您以前从未制作过GUI程序,那么尝试将控制台程序转移到GUI程序并不是一件容易的事。您需要了解事件驱动的编程。我建议您查看Swing tutorials
但有些提示。你想要一个“半gui”程序。您只需使用JOptionPane
s作为输入。假设你想得到一个数字输入。你会做这样的事情
String numberString = JOptionPane.showInputDialog(null, "Enter a Number");
int number = Integer.parseInteger(numberString);
执行第一行后,会自动弹出输入窗格。要求输入。结果是一个String,所以你有来解析它以得到一个数字。
此外,如果您只想 diplay 一条消息,请使用
JOptionPane.showMessageDialog(null, message);
你可以这样做,显示一些结果。在上述情况下,当您只想显示消息时,您不需要使其等于任何内容。因此,您可以使用System.out.println()
而不是JOPtionpane.showMesageDialog()
代替scan.next()
,而不是使用JOptionPane.showInputDialog()