Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这个if语句也不能实现我想要的效果?_Java_If Statement_Keyboard_Java.util.scanner_Conways Game Of Life - Fatal编程技术网

Java 为什么这个if语句也不能实现我想要的效果?

Java 为什么这个if语句也不能实现我想要的效果?,java,if-statement,keyboard,java.util.scanner,conways-game-of-life,Java,If Statement,Keyboard,Java.util.scanner,Conways Game Of Life,我正在制作一个生命游戏程序,它的早期阶段。当我运行程序并进入“DoyouwantMake…”并输入“y”时,它将转到else,打印test3的测试语句,然后结束程序。我忽略了什么 public static void main(String[] args) { Scanner kb = new Scanner(System.in); String userInput = ""; char[][] initialGrid = new char[25][75];

我正在制作一个生命游戏程序,它的早期阶段。当我运行程序并进入“DoyouwantMake…”并输入“y”时,它将转到else,打印test3的测试语句,然后结束程序。我忽略了什么

public static void main(String[] args) {

    Scanner kb = new Scanner(System.in);

    String userInput = "";
    char[][] initialGrid = new char[25][75];
    char[][] world = makeInitialGrid(kb, userInput, initialGrid);
    printGrid(world);
    userInput = "y";
    while (userInput == "y"){
        System.out.println("Do you want to make a new generation? (y) yes (n) no");
        userInput = kb.nextLine();
        System.out.println(userInput);
        if (userInput == "y"){
            System.out.println("test1");
            int numOfNeighbors = findNeighbors(world, 6, 2);
            System.out.println("test2");
            System.out.println(numOfNeighbors);
            //makeNewGeneration(world);
        } else {
            System.out.println("test3");
            break;

        }
    }
    kb.close();

对于Java中的字符串比较,需要使用
string#equals
,而不是
=
。尝试
if(userInput.equals(“y”){…

哦,是的……当然。非常感谢。没问题!如果这解决了您的问题,请记住接受答案。