在java中应用if/else语句时出错

在java中应用if/else语句时出错,java,Java,我的代码有问题,希望有人能看到我遗漏了什么。我的代码如下: import java.io.IOException; class Boat{ String boatName = (" "); boolean sailUp = false; } public class Project2{ public static void main(String[] args){ System.out.println("\n"); Boat[

我的代码有问题,希望有人能看到我遗漏了什么。我的代码如下:

import java.io.IOException;

class Boat{

    String boatName = (" ");
    boolean sailUp = false;

}

public class Project2{

    public static void main(String[] args){

        System.out.println("\n");

        Boat[] boatArray;

        boatArray = new Boat[args.length];

        for(int i = 0 ; i < args.length ; ++i){

            boatArray[i] = new Boat();

        }

        for(int j = 0 ; j < args.length ; ++j){

            boatArray[j].boatName = args[j];

        }

        for(int k = 0 ; k < args.length ; ++k){

            String firstLetter = boatArray[k].boatName.substring(0, 1);

            if(firstLetter == ("B")){

                boatArray[k].sailUp = true;

            }else if(firstLetter == ("C")){

                boatArray[k].sailUp = true;

            }else if(firstLetter == ("N")){

                boatArray[k].sailUp = true;

            }else{

                boatArray[k].sailUp = false;

            }
        }

        for(int l = 0 ; l < args.length ; ++l){

            System.out.println("\nThe " + boatArray[l].boatName + " is ready to sail...");

            if(boatArray[l].sailUp == false){

                System.out.println("\n\tbut the sail is down, raise the sail!");

            }else if(boatArray[l].sailUp == true){

                System.out.println("\n\tthe sail is up, ahead full!");

            }           
        }
    }
}
挑战者号已经准备好启航了

    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
发现号已经准备好启航了

    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
尼米兹号已经准备好启航了

    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
但我明白了:

C:\Documents and Settings>java Project2企业挑战者发现Nimitz

企业号准备启航了

    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
挑战者号已经准备好启航了

    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
发现号已经准备好启航了

    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
尼米兹号已经准备好启航了

    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    the sail is up, ahead full!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!
    but the sail is down, raise the sail!

为什么第三个循环没有重置sail状态?

字符串是对象,所以您可以使用
equals
方法(“C”.equals(firstLetter)),而不是
=
对它们进行比较

此外,如果您只需要一个字符,您可以提取字符(使用
charAt(int)
)并与“A”、“B”等进行比较(这次使用
=
:)。例如:


在处理字符串类型的对象时,请尝试使用“equals”或“equalsIgnoreCase”方法 “==”(也称为C样式相等)尝试比较对象引用

像这样的

if( "B".equalsIgnoreCase( firstletter ) ){
// do whatever
}
改变

firstLetter == ("B")

=
检查引用相等,其中as
.equals
将检查值相等

== -> checks for equality in reference.

equals -> checks for equality in value.

因此,请尝试使用equals方法。

当您将一个对象与另一个对象进行比较时,必须使用.equals()方法,但不能使用“==”。 “==”将比较两个字符串的指针。

像这样使用string.equals()

if(firstLetter.equals("B")){

       boatArray[k].sailUp = true;

}else if(firstLetter.equals("C")){

       boatArray[k].sailUp = true;
}else if(firstLetter.equals("N")){

       boatArray[k].sailUp = true;

}else{

       boatArray[k].sailUp = false;
}

谢谢你的帮助!我在这门课上才上了四个星期,甚至都不知道这种方法。我有很多东西要学!谢谢你的帮助!感谢您花时间审阅此内容,非常感谢!感谢您花时间审阅此内容,非常感谢!感谢您花时间审阅此内容,非常感谢!感谢您花时间审阅此内容,非常感谢!感谢您花时间审阅此内容,非常感谢!可能重复的