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和顏色,我嘗試過黑色和棕色。所有收到相同的錯誤
包括錯誤在你的問題 – KeithC
歡迎堆棧溢出!請[參觀](http://stackoverflow.com/tour)以查看網站的工作原理和問題,並相應地編輯您的問題。另請參閱:[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) –
它不允許我上傳錯誤圖像。然而,我得到的錯誤是沒有輸入正確的bin顏色 –