簡體   English   中英

我的if語句每次都會返回錯誤的值,這是我的數組嗎?

[英]my if statement returns the wrong value every time, is it my arrays?

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!"); )時,它都會顯示錯誤。 我已經搜索了可能的解決方案,並且我已經遍歷了整個代碼三遍,對您的幫助或指導將不勝感激。

java中的字符串是對象,因此執行String ==如果字符串是同一對象,則比較字符串,而不是包含相同值的字符串。 而是嘗試使用string1.equals(string2)

使用equalsequalsIgnoreCase比較Java中的字符串。 不要使用==運算符來比較String。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM