我的if语句每次都返回错误的值,是我的数组吗?

时间:2015-07-01 13:59:35

标签: java arrays if-statement

import java.util.Scanner;
public class Start
{

public static void main(String[] args) 
{
    Scanner reader = new Scanner(System.in);
    String name = null, helloworlduserattempt = null;
    Boolean helloworldcorrect;

    System.out.println("Welcome to the Java Teacher!");
    System.out.println();
    System.out.println("Type in your name");
    name = (reader.nextLine());
    System.out.println("First " + name + ", lets go over some basics. \nAnything you type to the console in Java Teacher will appear like this;");
    System.out.println();
    System.out.println("___________________________________________________________________________________________");
    System.out.println();
    System.out.println("*your printed text here*");
    System.out.println();
    System.out.println("___________________________________________________________________________________________");

    System.out.println("\n\nThe first exercise is called \"hello world\". This is the starting point for most people while learning how to code.\nThis simply entails writting the phrase \"hello world!\" to the console.");
    System.out.println("\nTo do this, we need to type in \"System.out.println(\"Hello World\");\"");
    System.out.println("\nLets have a look at what that actually means. System.out.println basically tells the system to output to the console the following message on a new line.\nYou could type print instead of println, but this would not write to a new line. Also, notice that System has a capital S");


    System.out.println("Now you try! type in what you think will print \"Hello World!\" to the console");

    do 
    {
        helloworlduserattempt = (reader.nextLine());

        String[] array = helloworlduserattempt.split("\""); 
        array[0] = array[0] + "\"";
        array[2] = "\"" + array[2];
        System.out.println(array[0] + array[1] + array [2]);
        if (helloworlduserattempt == "System.out.println(\"Hello World!\");")
        {
           System.out.println("___________________________________________________________________________________________");
    System.out.println("Hello World!");
    System.out.println("___________________________________________________________________________________________");
    System.out.println("Well done " + name  + "! that was perfect");
    helloworldcorrect = true;
        }
    else if (array[0] == "System.out.println(\"" && array[2] == "\");")
    {
    System.out.println("___________________________________________________________________________________________");
    System.out.println(array[1]);
    System.out.println("___________________________________________________________________________________________");
    System.out.println("youve got the code right " + name + ", but the actual text is incorrect");
    helloworldcorrect = false;
    }
    else 
        {
        System.out.println("___________________________________________________________________________________________");
        System.out.println("Error");
        System.out.println("___________________________________________________________________________________________");
        System.out.println("Oops! thats not quite right " + name + ", try again. remember you can look up at what i said earlier!");
        helloworldcorrect = false;
        }
    }
    while (helloworldcorrect = false);
}
}

每当我输入正确答案(System.out.println("Hello World!");)时,它都会显示错误。我已经搜索了可能的解决方案,现在我已经完成了三次整个代码,非常感谢任何帮助或指导。

2 个答案:

答案 0 :(得分:2)

java中的字符串是对象,因此如果它们是同一个对象,则执行String == String比较,而不是它们包含相同的值。而是尝试使用string1.equals(string2)

答案 1 :(得分:1)

使用equalsequalsIgnoreCase来比较java中的字符串。不要使用==运算符来比较字符串。