2017-05-04 50 views
0

由於某種原因,無論何時我在我的代碼中輸入其中一種顏色,它都會給我一個錯誤。任何人都可以看到的問題是在這裏輸出不工作,收到錯誤

什麼這是我的應用程序類:

import javax.swing.JOptionPane; 
public class binCollectionAPP{ 
    public static void main(String[] args){ 
     //Declare Variables 
     int apartmentNum, flag; 
     String collect, binColour; 

     //Objects 
     binCollection bin = new binCollection(); 

     //Inputs 
     apartmentNum=Integer.parseInt(JOptionPane.showInputDialog("Enter your apartment number")); 
     binColour=JOptionPane.showInputDialog("Enter the colour bin you are checking"); 

     //set 
     bin.setApartNum(apartmentNum); 
     bin.setBinColour(binColour); 

     //compute 
     bin.compute(); 

     //get 
     collect=bin.getCollectDay(); 
     flag=bin.getFlag(); 

     //output 
     if(flag == 1){ 
      JOptionPane.showMessageDialog(null,"Error, incorrect bin colour"); 
     } 
     else if(flag == 2){ 
      JOptionPane.showMessageDialog(null,"Error, incorrect apartment number"); 
     } 
     else{ 
      JOptionPane.showMessageDialog(null,"Your collection day is "+collect); 
     } 

    } 
} 

我的實例化類是:

public class binCollection{ 
    //Declare Vars 
    private int apartmentNum, flag; 
    private String collect, binColour; 

    //Constructor 
    public binCollection(){ 
     apartmentNum = 0; 
     flag = 0; 
     collect = ""; 
     binColour = ""; 
    } 

    //set methods 
    public void setApartNum(int apartmentNum){ 
     this.apartmentNum = apartmentNum; 
    } 
    public void setBinColour(String binColour){ 
     this.binColour = binColour; 
    } 

    //compute 
    public void compute(){ 
     if(apartmentNum % 2 == 0){ 
      if(binColour == "brown" || binColour == "Brown"){ 
       collect = "Monday"; 
      } 
      else if(binColour == "black"){ 
       collect = "Tuesday"; 
      } 
      else if(binColour == "green"){ 
       collect = "Wednesday"; 
      } 
      else{ 
       flag = 1; 
      } 
     } 
     else if(apartmentNum % 2 == 1){ 
      if(binColour == "brown"){ 
       collect = "Tuesday"; 
      } 
      else if(binColour == "black"){ 
       collect = "Wednesday"; 
      } 
      else if(binColour == "green"){ 
       collect = "Thursday"; 
      } 
      else{ 
       flag = 1; 
      } 
     } 
     else{ 
      flag = 2; 
     } 
    } 

    //get methods 
    public String getCollectDay(){ 
     return collect; 
    } 
    public int getFlag(){ 
     return flag; 
    } 
} 

我要去哪裏錯了?

關於錯誤: 我沒有收到編譯器錯誤,它全部編譯並運行,但在運行時輸入公寓號和顏色「棕色」後,我收到錯誤(代碼中的標誌2)

對於「輸入你的公寓號碼」,我輸入2和顏色,我嘗試過黑色和棕色。所有收到相同的錯誤

+3

包括錯誤在你的問題 – KeithC

+2

歡迎堆棧溢出!請[參觀](http://stackoverflow.com/tour)以查看網站的工作原理和問題,並相應地編輯您的問題。另請參閱:[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) –

+0

它不允許我上傳錯誤圖像。然而,我得到的錯誤是沒有輸入正確的bin顏色 –

回答

0

你的問題在於你如何檢查顏色是什麼。在Java中,通過.equals()方法(使用String==檢查物理相等性,這不是您想要的)完成字符串相等性。改變你的條件是

if (binColour.equals("brown") || binColour.equals("Brown")) { 
    collect = "Monday"; 
} 

或者,更簡單,嘗試使用.equalsIgnoreCase()

if (binColour.equalsIgnoreCase("brown")) { 
    collect = "Monday"; 
}