2

我的代码有问题,希望有人能看到我缺少的东西。我的代码如下:

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!");

            }           
        }
    }
}

我想要以下输出:

C:\Documents and Settings>java Project2 Enterprise Challenger Discovery 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!

但我明白了:

C:\Documents and Settings>java Project2 Enterprise Challenger Discovery Nimitz

企业已准备好启航...

    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!

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

4

6 回答 6

4

字符串是对象,因此您可以使用equals方法 ("C".equals(firstLetter)) 来比较它们,而不是==.

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

char firstLetter = boatArray[k].boatName.charAt(1);
if(firstLetter == 'B'){
于 2012-05-08T04:46:01.317 回答
3

改变

firstLetter == ("B")

firstLetter.equals("B")

ETC

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

于 2012-05-08T04:47:38.727 回答
0

在处理String类型的对象时尝试使用方法 'equals' 或 'equalsIgnoreCase' A '=='(也称为 C 样式相等)尝试比较对象引用。

像这样的东西

if( "B".equalsIgnoreCase( firstletter ) ){
// do whatever
}
于 2012-05-08T04:46:28.217 回答
0
== -> checks for equality in reference.

equals -> checks for equality in value.

所以尝试使用 equals 方法。

于 2012-05-08T04:48:07.263 回答
0

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

于 2012-05-08T04:50:22.197 回答
0

像这样使用 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;
}
于 2012-05-08T04:53:55.537 回答