简体   繁体   English

即使条件为真也不执行 if 语句

[英]not executing the if statement even when the condition is true

I have made a java code to play hangman game.我制作了一个java代码来玩刽子手游戏。 I have given the user 15 chances to guess the word and once the user has guessed the correct answer it should execute the if statement but it does not.我给了用户 15 次机会来猜这个词,一旦用户猜到了正确的答案,它应该执行 if 语句,但它没有。 I have tried to solve this problem many times but it is never working.我曾多次尝试解决这个问题,但它从未奏效。 I would appreciate it if you could tell me the problem and give a suitable solution without making much change in my code.如果您能告诉我问题并提供合适的解决方案而不对我的代码进行太多更改,我将不胜感激。

My code:我的代码:

import java.util.*;
public class game
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        String list[] = {"apple", "banana", "mango", "kiwi", "coconut", "papaya", "lichi", "strawberry", "orange", "cherry"};
        int rand = (int)(Math.random()*9)+0;
        String word = list[rand];
        String ask = "_";
        for(int i = 1; i < word.length();i++){
            ask = ask + "_";
        }
        System.out.println(ask);
        System.out.println("hint: It is a fruit");
        ArrayList<String> ans = new ArrayList<String>();
        for (char i : ask.toCharArray()){
            ans.add("_");
        }
        for (int j = 1; j<=15; j++){
            System.out.println("Enter a character: ");
            String input = in.next();
            char alt = input.charAt(0);
            int x = 0;
            for (char i : word.toCharArray()){
                if(alt == i){
                    ans.set(x, input);
                }
                x++;
            }
            for (String i : ans){
                System.out.print(i);
            }
            int y = 0;
            ArrayList<String> answer = new ArrayList<String>();
            for (char i : word.toCharArray()){
                answer.add("_");
            }
            for(char i : word.toCharArray()){
                String alternate = String.valueOf(i);
                answer.set(y, alternate);
                y++;
            }
            if (ans == answer){
                System.out.println("\nyou win");
                break;
            }
            System.out.println("\n"+ans);
            System.out.println(answer
            );
        }
    }
}

Due to so many many unsuccessful attempts my code may have some unnecessary lines which are making the long.由于有太多不成功的尝试,我的代码可能有一些不必要的行,这些行变得很长。

when you use == it compares whether these two reference variables are pointing to the same object or not.当您使用==它会比较这两个引用变量是否指向同一个对象。 if you want to compare their content then you should use the equals() method which compares the content of the object, not the object itself.如果你想比较它们的内容,那么你应该使用equals()方法来比较对象的内容,而不是对象本身。

use ans.equals(answer) instead of ans== answer使用ans.equals(answer)而不是ans== answer

尝试使用equals()方法而不是==

if (ans.equals(answer)){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM