所以我正在尝试制作一个刽子手游戏,它从单词文件中生成一个随机单词,向用户询问一个字母,然后遍历该单词并在它出现的地方打印字母或打印一个“_”。我知道我的“抱歉没有找到匹配项”现在是不正确的,但是当我打印这个单词时,我无法将最后一个正确的字母保持在适当的位置
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class hangman
{
public static void main(String args[])
{
Scanner hangman = null;
try
{
// Create a scanner to read the file, file name is parameter
hangman = new Scanner (new File("C:\\Users\\Phil\\Desktop\\hangman.txt"));
}
catch (FileNotFoundException e)
{
System.out.println ("File not found!");
// Stop program if no file found
System.exit (0);
}
String[] list = new String[100];
int x = 0;
while(hangman.hasNext())
{
list[x] = hangman.nextLine();
x++;
}
Random randWord = new Random();
String word = "";
int wordNum = 0;
boolean stillPlaying = true;
wordNum = randWord.nextInt(12);
word = list[wordNum];
System.out.println("The word has "+word.length()+" letters");
Scanner letter = new Scanner(System.in);
String guess = "";
while(stillPlaying = true)
{
System.out.println("Guess a letter a-z");
guess = letter.nextLine();
for(int y = 0; y<word.length(); y++)
{
if(word.contains(guess))
{
if(guess.equals(word.substring(y,y+1)))
{
System.out.print(guess+" ");
}
else
System.out.print("_ ");
}
else
{
System.out.println("Sorry, no matches found");
}
}
}
}
}