【发布时间】:2016-02-21 00:07:06
【问题描述】:
大家好,我在 if 语句中的代码遇到了问题。无论我输入什么 if 语句总是将 f_resist [] 返回为 0。我认为我的 if 语句有一个错误,但我不确定:(
Scanner u_scancolor = new Scanner(System.in); //declare input box
System.out.println("Enter three colors on the resistor seperated by hyphens. ie: Red-Blue-Brown");
System.out.println("Colours you can use: Black, Brown, Red, White, Orange, Yellow, Green, Grey, Violet, Blue");
String f_wire = u_scancolor.next(); //takes a string
System.out.println(f_wire);
int [] f_resist;
f_resist = new int [3];
String [] f_wordcolor= f_wire.split("-"); //splits the colors by hyphen into individual strings
for (int cnt1=0 ; cnt1<3; cnt1++) {
f_wordcolor [cnt1] = f_wordcolor [cnt1].toUpperCase();
System.out.println(f_wordcolor [cnt1]);
if (f_wordcolor [cnt1] == "BLACK ") {
f_resist [cnt1] = 0;
}
else if (f_wordcolor [cnt1] == "BROWN") {
f_resist [cnt1] = 1;
}
else if (f_wordcolor [cnt1] == "RED") {
f_resist [cnt1] = 2;
}
else if (f_wordcolor [cnt1] == "ORANGE") {
f_resist [cnt1] = 3;
}
else if (f_wordcolor [cnt1] == "YELLOW") {
f_resist [cnt1] = 4;
}
else if (f_wordcolor [cnt1] == "GREEN") {
f_resist [cnt1] = 5;
}
else if (f_wordcolor [cnt1] == "BLUE") {
f_resist [cnt1] = 6;
}
else if (f_wordcolor [cnt1] == "VIOLET") {
f_resist [cnt1] = 7;
}
else if (f_wordcolor [cnt1] == "GREY") {
f_resist [cnt1] = 8;
}
else if (f_wordcolor [cnt1] == "WHITE") {
f_resist [cnt1] = 9;
}
System.out.println(f_resist [cnt1]);
}
String f_add1 = Integer.toString(f_resist [0]);
String f_add2 = Integer.toString(f_resist [1]);
String f_stringadd = f_add1 + f_add2;
int f_intadd = Integer.parseInt(f_stringadd);
int f_ohm = f_intadd*10^f_resist[2];
System.out.println("Total ohms: " + f_ohm);
}
}
【问题讨论】:
-
请注意..您在“BLACK”中有一个额外的空间
标签: java arrays if-statement