package Hangman;
import java.util.*;
public class Hangman {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Setup Variables
String name;
int diff, countwrong=0, guesses=0;
Scanner input = new Scanner (System.in);
无聊的介绍部分。
//Introduction for the Hangman game.
System.out.println(" Welcome to Hangman. What is your name?");
name=input.nextLine();
System.out.println(" Hello "+name+" You will be playing a Hangman game.");
System.out.println(" ");
//Give instructions.
System.out.println(" This game will have basics Hangman rules.");
System.out.println(" There will be a certain number of blanks on the screen, resembling the letters of a particular word.");
System.out.println(" You will have to try and determine the word by guessing letters used in the word.");
System.out.println(" You will be allotted 7 mistakes to find the word");
System.out.println(" ");
System.out.println(" There will be 3 difficulties you can choose form and it will get progressively harder as you go higher.");
System.out.println(" 1 being words with 5 letters, 2 being words with 7 letters, and 3 being words with 9 letters.");
System.out.println(" ");
//Ask user which difficulty they would like to play on.
System.out.println(" Which difficulty do you want? (1-3)");
diff=input.nextInt();
我将使用的词。
//Setup level arrays.
String [] wordsl1 = {"apple","truck","phone","paper","funny"};
String [] wordsl2 = {"garbage","program","variety","comment","teacher"};
String [] wordsl3 = {"yesterday","geography", "character","knowledge", "chemistry"};
//Setup main array.
String [] gameword = new String [5];
//Loop for them to choose difficulty.
for (int i=0;i<5;i++){
//If they user chose the easiest difficulty.
if (diff==1){
//Put Level one words into game array.
gameword [i] = wordsl1[i];
}
//If they user chose the medium difficulty.
else if (diff==2){
//Put Level one words into game array.
gameword [i] = wordsl2[i];
}
//If they user chose the hardest difficulty.
else if (diff==3){
//Put Level one words into game array.
gameword [i] = wordsl3[i];
}
}
//Randomly choose 1 word.
int n=(int)(Math.random()* 6+ 0);
String guessword = gameword[n];
//Find the length of the word in the array.
String leng = guessword;
//Out put the number of letters in the word.
System.out.println(" There are "+leng.length()+" letters in the word.");
System.out.println(" ");
//Create an array for the individual letters.
String [] secretword = new String [leng.length()];
//Create a for loop to add the words to the array.
for (int i=0;i<leng.length();i++){
//Add each letter to the secret array.
secretword[i] = guessword.substring(i,i+1);
}
//Create an array for the blanks replacing the letters.
String [] temp = new String [leng.length()];
//Create a for loop to add the blanks to the temporary array.
for (int i=0;i<leng.length();i++){
//Fill the array with the dashes.
temp[i] = "_";
//Print the array
System.out.print(temp[i]+" ");
}
//While statement to check if its over.
while (countwrong!=6 || gameword[n] != temp[n] || guesses!=26) {
System.out.println(" ");
//Ask the user to guess a letter.
System.out.println(" Guess a letter:");
String a = input.next();
//For loop to check the letter to see if it is part of the word.
for (int i=0;i<leng.length();i++){
这是让我一直搞砸的部分。我知道这是错的,我不知道该怎么办!
**//Check if the letter is in the word.
//If it is.
if (a == guessword.substring(i,i+1)){
System.out.println("This letter is in the "+guessword.indexOf(a)+"th place.");
temp[i] = temp[i].replace("_", a);
}
//If it isn't.
else {
//Increase the wrong count by one.
countwrong++;
}
我只应该使用上面得到的 a 的索引来替换单个部分,但我不知道该怎么做。
//Print the modified array.
System.out.print(temp[i]+" ");
}**
//Increase number of guesses by one.
guesses++;
}
无聊的奥特罗。
//If the user guessed correctly.
if (gameword[n] == temp[n]){
System.out.println(" You have guessed the word correctly! Congratulations!");
}
//If the user could not guess the word.
else if (countwrong == 6){
System.out.println(" Sorry. You have not guessed the word.");
System.out.println(" The word was "+guessword+" .");
}
//Output the number of guesses the user took and how many times they got one wrong.
System.out.println(" You took "+guesses+" guesses to get it right.");
System.out.println(" You got a incorrect letter "+countwrong+" times.");
}
}