gpt4 book ai didi

java - 简单的绞刑吏游戏 - 3 个问题

转载 作者:行者123 更新时间:2023-12-02 04:45:46 24 4
gpt4 key购买 nike

好的,这是我的第一个 Java 项目。我很高兴它终于起作用了,特别是因为我在上面投入的时间比我想象的要多得多。

我有 3 个问题

  1. 我必须将“static”放在所有方法共享的变量前面,因为如果没有它,我就会出现“无法从静态内容引用非静态变量”错误。它对我的脚本有何影响?它在实践中有什么意义吗?

  2. 为什么我无法更改包名称?除了脚本的第一行之外,没有在任何地方提到它

  3. 游戏可以运行,但还有一件小事我忘记了,我真的不知道如何解决这个问题。

这部分决定玩家是否获胜

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

问题是,我必须写数字“6”,因为我承认在我的代码字“Economy”中有 2 个字母“o”,所以如果会这样

wordList.size() == word.length() 

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

完整代码:

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");



}
}




}

感谢您的帮助!

最佳答案

  1. 静态关键字意味着类拥有您的字段,而不是对象本身。这意味着该字段不会随着每个对象创建而创建,而是只有一个字段在给定类型的所有对象之间共享。您必须仅在主应用程序类中执行此操作,因为主方法是静态的。
  2. 可以,您只需相应地更改目录结构即可。
  3. 获取单词,跳过相同的字母,计算长度,然后与您猜测的字母进行核对。

关于java - 简单的绞刑吏游戏 - 3 个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56484004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com