java不会进入for循环(不包括==)

标签 java eclipse loops for-loop

我正在尝试用java编写一个与用户一起玩刽子手的程序。但是,用于检查字母是否正确的 for 循环不起作用。我很难确定原因。我使用的程序是Eclipse。这里只是 for 循环和包含它的 if 语句(l 是单词和数组列表 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);

        }


    }
}

我浏览了这个网站上的几个“for 循环未输入”类型的问题,但我看到的所有问题都以 i == x 作为终止要求。正如您所看到的,这里的情况并非如此。

预先感谢您提供的任何帮助!

最佳答案

尽管您的循环可能不使用 ==,但您的 if 条件确实使用 == 来比较字符串,这是一个很大的禁忌。将字符串与 == 比较引用是否相等(它们是内存中完全相同的字符串吗?)。使用 String#equals 将产生正确的值(它们是否具有相同的字母序列?)比较:

if (g.equals(theWord.get(k)))

关于java不会进入for循环(不包括==),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25416718/

相关文章:

java - 默认情况下,UDP(在 Java 或其他语言中)是全双工的吗?

java.lang.NoSuchMethodError : Graph: method <init>()V not found

php - 如何安排PHP for循环值?

java - 如何将 ArrayList 传递给以集合作为输入的方法

java - 为什么浮点值在 sysout 时自动递增

eclipse - 使用Groovy-Eclipse插件在Grails中进行开发

c++ - 数组索引循环开始而不是内存访问错误

c++ - 由围绕矩形顺时针方向移动的数字组成的图案(长度和宽度每次都减小)

java - 用整数/BigInteger 与 BigDecimal 表示金钱

java - exec-maven-plugin 未检测到 src/main/java 中的类