出于某种原因,每当我在代码中输入一种设定颜色时,它就会给我一个错误。任何人都可以看到这里的问题
这是我的app类:
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,对于颜色,我尝试过黑色和棕色。全部收到相同的错误
答案 0 :(得分:0)
您的问题在于如何检查颜色。在Java中,使用import ast
with open('C:\\Users\\Moondra\Anaconda_related\Jupyer_notebooks\MachineLearning\Small_cap_Bio.txt', 'r') as f:
my_list = ast.literal_eval(f.read())
方法完成字符串相等性(使用.equals()
,String
检查物理相等性,这不是您想要的。改变你的条件
==
或者,为了更加简单,请尝试使用if (binColour.equals("brown") || binColour.equals("Brown")) {
collect = "Monday";
}
.equalsIgnoreCase()