首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单Hangman游戏-3个问题

简单Hangman游戏-3个问题
EN

Stack Overflow用户
提问于 2019-06-06 19:32:01
回答 1查看 43关注 0票数 0

这是我第一个用Java做的项目。我很高兴它终于成功了,尤其是因为我投入了比我想象中更多的时间。

不过,我有三个问题

  1. 我必须将‘静态’放在所有方法共享的变量前面,因为没有它,我就有“不能从静态内容引用非静态变量”的错误。它对我的剧本有什么影响,在实践中有什么意义吗?
  2. 为什么我不能更改包名?除了脚本的第一行之外,这里没有提到它。
  3. 这个游戏很管用,但还有一件事我忘了,我真的不知道如何解决这个问题。

这个部分决定玩家是否赢了。

代码语言:javascript
复制
 if  (wordList.size() == 6) {
                System.out.println("\nYou won, congratulations! \n");
                break;
 }

问题是,我不得不写数字'6‘,因为我承认在我的代码单词'Economy’中有两个字母'o',所以如果会这样

代码语言:javascript
复制
wordList.size() == word.length() 

它不起作用(如果一个码字中没有两个相同的字母,它就会起作用)

完整代码:

代码语言:javascript
复制
package hangman;
import java.util.Scanner;
import java.util.ArrayList;

public class Hangman {
        static Scanner reader = new Scanner(System.in);
        static ArrayList<String> wordList = new ArrayList<String>();
        static ArrayList<Character> wordListChar = new ArrayList<Character>();
        static String word = "economy";
        static int answers = 0;

public static void main(String[] args) {

        System.out.println("************");
        System.out.println("* Hangman *");
        System.out.println("************");
        System.out.println("");
        System.out.println("");


        while (true)  {

        System.out.println("Choose a letter! \n");
        String command = reader.nextLine(); 
            if (command.length() == 1) {

                if (!wordList.contains(command)) {
                   printWord(command);
                   guess(command);
                }
                else {
                    System.out.println("You already guessed this letter! \n");
                }
            }
            else {
                System.out.println("Write only 1 letter!");
            }

            if  (wordList.size() == 6) {
                System.out.println("\nYou won, congratulations! \n");
                break;
            }

            if  (answers == 6) {
                System.out.println("\nThis guy is dead, You lost! \n");
                break;
            }

        }

    }

public static void guess(String command) {

      if (word.contains(command)) {
          System.out.println("\nYes, the letter - " + command + " - is in the word!\n" );


      } else {
          System.out.println("\nThe letter - " + command + " - is NOT in the word!\n" );
          answers++;
          hangHim();
      }

}

public static void hangHim() {
    if (answers == 1) { 
                System.out.println("         ____________");
                System.out.println("        |      |     |");
                System.out.println("        |      O     |");
                System.out.println("        |            |");
                System.out.println("        |            |");
                System.out.println("        |            |");
                System.out.println("         ____________");
                System.out.print("\n");
    } 
    else if (answers == 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.print("\n");
    } 
    else if (answers == 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.print("\n");
    } 
    else if (answers == 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.print("\n");
    } 
    else if (answers == 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.print("\n");
    } 
    else if (answers == 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.print("\n");


    } 

}

public static void printWord(String command) {

       if (word.contains(command)) {


                        wordList.add(command);

                        String command2 = command;
                        char commandChar = command2.charAt(0);
                        wordListChar.add(commandChar);

                       // System.out.println(wordList.size());
                       // System.out.println(wordListChar.size());

                        for (int i = 0; i < word.length(); i++) {
                            char letter = word.charAt(i);

                            if (command.charAt(0) == letter) {
                                System.out.print(letter); 
                            }
                           else if (wordListChar.contains(word.charAt(i))) {
                                System.out.print(letter);
                            }
                            else {
                                System.out.print('*');

                            }
                        }
                      System.out.print("\n\n");



            }    
    }




}

谢谢你帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-06 20:56:05

  1. 静态关键字意味着类拥有您的字段,而不是对象本身。这意味着该字段将不会在每个对象创建时创建,而是在给定类型的所有对象之间只有一个字段共享。您只能在主应用程序类中这样做,因为main方法是静态的。
  2. 可以,您只需相应地更改目录结构。
  3. 取单词,跳过相同的字母,数一数长度,然后根据你猜到的字母检查它。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56484004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档