好的,所以我正在用java制作一个刽子手游戏,但我唯一的问题是程序没有正确显示实际的“刽子手”。这是我的代码:
import java.io.File;
import java.util.*;
import javax.swing.JOptionPane;
public class Hangman1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
Random ran = new Random();
Scanner file = new Scanner(new File("word.txt"));
String dictionary = file.nextLine();
char array2[];
String wordlist[] = {"accoutrements","acumen","anomalistic","auspicious","bellwether","callipygian","circumlocution"};
int number = ran.nextInt(7);
String in = null;
in = wordlist[number];
//System.out.println(number);
String guess;
int numofchances = 9,k=0,p=0,q=0;
array2 = new char[numofchances+1];
boolean array[],b=true;
array = new boolean[in.length()];
for (int u=0;u<in.length();u++)
{
array[u]=false;
}
while(numofchances >= 1 && b){
numofchances--;
System.out.println("Enter your guess: ");
guess = input.nextLine();
if (q==0)
{
System.out.println("-------------");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
else if (q==1)
{
System.out.println("-------------");
System.out.println("| | ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
else if(q==2)
{
System.out.println("-------------");
System.out.println("| | ");
System.out.println("| o ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
else if(q==3)
{
System.out.println("-------------");
System.out.println("| | ");
System.out.println("| o ");
System.out.println("| / ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
else if(q==4)
{
System.out.println("-------------");
System.out.println("| | ");
System.out.println("| o ");
System.out.println("| /| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
else if(q==5)
{
System.out.println("-------------");
System.out.println("| | ");
System.out.println("| o ");
System.out.println("| /|\\ ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
else if(q==6)
{
System.out.println("-------------");
System.out.println("| | ");
System.out.println("| o ");
System.out.println("| /|\\ ");
System.out.println("| | ");
System.out.println("| ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
else if(q==7)
{
System.out.println("-------------");
System.out.println("| | ");
System.out.println("| o ");
System.out.println("| /|\\ ");
System.out.println("| | ");
System.out.println("| / ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
else if(q==8)
{
System.out.println("-------------");
System.out.println("| | ");
System.out.println("| o ");
System.out.println("| /|\\ ");
System.out.println("| | ");
System.out.println("| / \\ ");
System.out.println("| ");
System.out.println("|");
System.out.println("-------------");
}
char character = guess.charAt(0);
array2[k] = character;
k++;
p = 0;
for(int i=0;i<in.length();i++){
char c = in.charAt(i);
if(c == character || array[i]==true){
System.out.print(c+" ");
array[i] = true;
p = 1;
}else{
System.out.print("_ ");
}
}
if (p==0)
q++;
System.out.println();
for(int i=0;i<k;i++){
System.out.print(array2[i]+" ");
}
System.out.println();
b = false;
System.out.println();
for (int i=0;i<in.length();i++){
if (array[i]==false)
b=true;
}
}
if(b){
System.out.println("You lose.");
}
else{
System.out.println("You won.");
}
}
}
程序总是输出“刽子手”的帧,当猜错字母时它不会输出其余的图片。