簡體   English   中英

在Java中應用if / else語句時出錯

[英]Error applying the if/else statement in 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[] 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企業挑戰者發現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企業挑戰者發現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!

為什么第三循環不重置帆狀態?

字符串是對象,因此您使用equals方法(“ C” .equals(firstLetter))而非==

同樣,如果只需要一個字符,則可以提取char(使用charAt(int) )並與'A','B'等進行比較(這次使用== :)),例如:

char firstLetter = boatArray[k].boatName.charAt(1);
if(firstLetter == 'B'){

更改

firstLetter == ("B")

firstLetter.equals("B")

等等

==檢查引用是否相等,其中.equals將檢查值是否相等

在處理字符串類型的對象時,嘗試使用方法“等於”或“ equalsIgnoreCase”。“ ==”(也稱為C樣式相等)試圖代替比較對象引用。

像這樣的東西

if( "B".equalsIgnoreCase( firstletter ) ){
// do whatever
}
== -> 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;
}

暫無
暫無

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

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