我正在使用if语句:
if(test == 1 || test == 2){
do something
}
Run Code Online (Sandbox Code Playgroud)
我在Java工作,不知何故这个代码产生"坏操作数类型"的错误.我知道它是OR(||),但我不知道如何解决它.码:
public static int[] map =
//1 2 3 4 5 6 7 8 9 10
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //1
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //2
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //3
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //4
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //5
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //6
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //7
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //8
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //9
};
public static int mapRow, mapCol;
public static int mapRef = (mapRow + (mapCol * 10)) - 1;
String grass = "[ ] ";
String water = "{} ";
private int counter = 0;
void mapCreate(){
while(counter != 99){
if((counter = 0) || (counter = 10) || (counter = 20) || (counter = 30) || (counter = 40) || (counter = 50)
|| (counter = 60) || (counter = 70) || (counter = 80) || (counter = 90) || (counter = 100)){
if(map[counter] == 1){
System.out.println(grass);
} else if(map[counter] == 2){
System.out.println(water);
}
} else {
if(map[counter] == 1){
System.out.print(grass);
} else if(map[counter] == 2){
System.out.print(water);
}
}
counter++;
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
mapcreater.java:27: error: bad operand types for binary operator '||'
if((counter = 0) || (counter = 10) || (counter = 20) || (counter = 30) || (counter = 40) || (counter = 50)
Run Code Online (Sandbox Code Playgroud)
不要将int值与=赋值运算符进行比较.使用==比较,这会导致需要boolean.更改
if((counter = 0) ||
Run Code Online (Sandbox Code Playgroud)
至
if((counter == 0) || // And the others after it also.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |