我正在尝试用java编写一个与用户一起玩hangman的程序。但是,检查字母是否正确的for循环不起作用。我很难确定原因。我正在使用的程序是Eclipse。这里只是for循环和if语句的封装(l是单词和arraylist theGuessed的长度,g是猜测的字母):
if (theWord.contains(g))
{
for (int k = 0; k > l; k++)
{
if (g == theWord.get(k))
{
theGuessed.remove(k);
theGuessed.add(k, g);
b = "true";
System.out.println(theGuessed);
}
}
以下是整个代码:
import java.util.Scanner;
import java.util.ArrayList;
public class HangMan
{
private static String guesses;
private static String b;
private static String e;
private static String m;
private static String n;
private static String o;
private static String p;
private static String q;
private static String r;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ArrayList<String> theWord = new ArrayList<String>();
ArrayList<String> theGuessed = new ArrayList<String>();
guesses = "";
b = "";
e = "";
m = "";
n = "";
o = "";
p = "";
q = "";
r = "";
System.out.println("Player 1, how many letters are in your word?");
int l = input.nextInt();
System.out.println("What is the word that Player 2 should try to guess? Type one letter at a time.");
String d = input.nextLine();
theWord.add(0, d);
for (int i = 0; i< l; i++)
{
d = input.nextLine();
theWord.add(i, d);
}
for (int j = 0; j< l; j++)
{
theGuessed.add(j, "_");
}
System.out.println(theGuessed);
int y = 0;
while(y == 0)
{
System.out.println("Player 2! What is your guess?");
String g = input.nextLine();
if (theWord.contains(g))
{
for (int k = 0; k != l; k++)
{
if (g == theWord.get(k))
{
theGuessed.remove(k);
theGuessed.add(k, g);
b = "true";
System.out.println(theGuessed);
}
}
if(theGuessed == theWord)
{
System.out.println("That's the word! You win!");
y++;
}
}
else if (m == "wrong" && n == "wrong" && o == "wrong" && p == "wrong" && q == "wrong" && r == "wrong")
{
System.out.println(" |");
System.out.println(" O");
System.out.println("/ | \\");
System.out.println(" / \\");
System.out.println("You lose! The word was " + theWord);
y++;
}
else if (m == "wrong" && n == "wrong" && o == "wrong" && p == "wrong" && q == "wrong")
{
System.out.println(" |");
System.out.println(" O");
System.out.println("/ | \\");
System.out.println(" /");
r = "wrong";
}
else if (m == "wrong" && n == "wrong" && o == "wrong" && p == "wrong")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
System.out.println(" O");
System.out.println("/ | \\");
q = "wrong";
}
else if (m == "wrong" && n == "wrong" && o == "wrong")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
System.out.println(" O");
System.out.println("/ |");
p = "wrong";
}
else if (m == "wrong" && n == "wrong")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
System.out.println(" O");
System.out.println("/");
o = "wrong";
}
else if (m == "wrong")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
System.out.println(" O");
n = "wrong";
}
else if (b != "true")
{
System.out.println("That letter is not in the word!");
System.out.println(" |");
m = "wrong";
}
guesses = guesses + g + ", ";
System.out.println("Your guesses so far: " + guesses);
}
}
}
我查看了几个&#34; for循环未输入&#34;在这个网站上输入问题,但我看到的所有问题都有i == x作为他们的终止要求。如你所见,这不是这种情况。
提前感谢您提供的任何帮助!
答案 0 :(得分:3)
即使你的循环可能没有使用==
,你的if条件确实使用==
来比较字符串,这是一个很大的禁忌。将引用与==
相等的字符串进行比较(它们是内存中完全相同的字符串吗?)。使用String#equals
将得到一个正确的值(它们是否具有相同的字母序列?)比较:
if (g.equals(theWord.get(k)))