0

好的,所以我正在用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.");
    }



}
}

程序总是输出“刽子手”的帧,当猜错字母时它不会输出其余的图片。

4

2 回答 2

0

JOptionPane将 aString作为参数显示给它。您不能使用 println() 打印到它。

您需要将 String 与\n's 连接起来,而不是打印。像这样的东西。

import javax.swing.JOptionPane;

public class Test {
    public static void main(String[] args){
       String hangman = 
           "-------------" + "\n" +
           "|        |   " + "\n" +
           "|        o   " + "\n" +
           "|       /|\\ " + "\n" +
           "|        |   " + "\n" +
           "|       / \\ " + "\n" +
           "|            " + "\n" +
           "|" + "\n" +
           "-------------";

      JOptionPane.showMessageDialog(null,hangman);
   }
}

测试这个简单的程序,看看我在说什么。

于 2013-11-15T03:18:09.917 回答
0

您正在打印的吊死人的版本是基于q变量的,因此每次在单词中找不到猜测的字母时它自然应该递增,但只有在没有找到猜测的字母时才会递增这个单词。如果你只输入错误的字母,你会看到这个人一点一点地出现。

问题是递增q由 控制p==0p如果 ,则设置为 1 c == character || array[i]==true,其中array是所有先前正确猜测的字母的记录。您只需要设置p = 1when c == character,而不是其他任何东西。

您可能还需要考虑使用更具描述性的变量名称,因为这可以帮助您发现这类问题。

于 2013-11-15T05:05:53.220 回答