所以,我已經成功地製作了一個非常簡單但完整的hang子手版本。現在我想改進程序並慢慢添加功能(最終是我希望的圖形)。我想解決的第一件事就是重複字母,在這一點上,如果用戶決定鍵入單詞長度的同一個字母(單詞中的一個),他們會得到虛假的「勝利」。阻止這種情況發生的最好方法是什麼?hang子手遊戲中的bug
ps。我是編程新手,這個項目對我來說真的很難......所以如果答案真的很明顯,對不起問題,我今晚不能再想了。
任何幫助,將不勝感激,這裏是我的代碼:
import java.io.*;
public class hangman_test2
{
public static void main(String[] args) throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
boolean Lets_play=true;
String response;
while (Lets_play)
{
printheader();
String the_word=getWord();
System.out.println(the_word);
print_blanks(the_word);
guesses(the_word);
System.out.println("Want to play again?");
response=in.readLine();
if(response.charAt(0)=='n' || response.charAt(0)=='N')
{
Lets_play= false;
System.out.println("Thanks for playing!");
}
}
}//end main
public static void printheader()
{
System.out.println("Welcome, lets play hangman!");
System.out.println("enter letters to guess the word\n");
}//end print header
public static String getWord()
{
String [] possible_word=new String [10];
possible_word[0]="green";
possible_word[1]="orange";
possible_word[2]="tree";
possible_word[3]="flowers";
possible_word[4]="ocean";
possible_word[5]="grudge";
possible_word[6]="scraple";
possible_word[7]="crab";
possible_word[8]="insect";
possible_word[9]="stripes";
String theWord= possible_word [(int)(Math.random()*possible_word.length)];
return theWord;
}//end the word
public static void print_blanks(String the_word)
{
for (int x=0; x<the_word.length(); x++)
{
System.out.print("_ ");
}
}//print blanks
public static void guesses(String the_word)throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
boolean thisRound=true;
int strike=0;
int right_letter=0;
while (thisRound)
{
int letters_not_in_word=0;
char letter_guessed=in.readLine().charAt(0);
for (int current_letter=0; current_letter<the_word.length(); current_letter++)
{
if (the_word.charAt(current_letter)==letter_guessed)
{
System.out.println(letter_guessed + " fits in space number " + (current_letter+1));
right_letter++;
if(right_letter == the_word.length())
{
win(the_word);
thisRound=false;
}
}
else
{
letters_not_in_word++;
if (letters_not_in_word==the_word.length())
{
System.out.println(letter_guessed + " is not in the word");
strike ++;
if(strike==5)
{
lose(the_word);
thisRound=false;
}
}//if
}//else
}//for
}//while
}//end guesses
public static void win(String word)
{
System.out.println("\ncongradulations, you won!");
System.out.println("the word is " + word + "\n");
}
public static void lose(String word)
{
System.out.println("\nsorry, you lost");
System.out.println("the word is " + word + "\n");
}
}
你是Java新手,這就是爲什麼我告訴你。您沒有遵循適當的命名約定。您的班級名稱「hangman_test2」應以大寫字母開頭,並且不需要像「HangmanTest2」那樣放置_,並且變量名應以小寫字母「letsPlay」開頭。 – gprathour
我還沒有看過代碼,但是你不能設置它,以便如果他們輸入多個字符,那麼只有當他們輸入的是單詞時纔給他們一個贏。 – mowwwalker