好的,这是我的第一个Java项目。我很高兴它终于成功了,特别是因为我花了很多时间在我想不到的时间内。
我有3个问题
我必须在所有方法共享的变量前面放置“静态”,因为如果没有它,我将遇到“无法从静态内容引用非静态变量”错误。它如何影响我的脚本,在实践中有什么意义?
为什么我不能更改软件包名称?除了脚本的第一行之外,没有其他地方提到它
游戏可以正常运行,但是我忘了一件事,而且我真的不知道如何解决这个问题。
这部分决定玩家是否获胜
if (wordList.size() == 6) {
System.out.println("\nYou won, congratulations! \n");
break;
}
问题是,我不得不写数字“ 6”,因为我承认在我的代码字“经济”中有2个字母“ o”,所以是否可以这样写?
wordList.size() == word.length()
它不起作用(如果代码字中没有两个相同的字母,它将起作用)
完整代码:
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");
}
}
}
感谢帮助!
答案 0 :(得分:0)