CodeGym /Java 博客 /随机的 /Java 中的数字运算符
John Squirrels

San Francisco

# Java 中的数字运算符

``````
public class Main {

public static void main(String[] args) {

int x = 999;
int y = 33;

System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y);
}
}
``````

``````
public class Main {

public static void main(String[] args) {

int x = 33;
int y = 33%2;
System.out.println(y);
}
}
``````

• 等于 ( `==`)
• 大于 ( `>`)
• 小于 ( `<`)
• 大于或等于 ( `>=`)
• 小于或等于 ( `<=`)
• 不等于 ( `!=`)

``````
public class Main {

public static void main(String[] args) {

int x = 33;
int y = 999;
System.out.println(x=y);// We expect false to be displayed
}
}
``````

``````
public class Main {

public static void main(String[] args) {

int x = 33;
int y = 999;
System.out.println(x==y);
}
}
``````

``````
public class Main {

public static void main(String[] args) {

int x = 999;
int y = 33;
int z = 256;

x = y = z;
System.out.println(x);
}
}
``````

• `y = z`， 那是，`y = 256`
• `x = y`， 那是，`x = 256`

## 一元运算符。

• 一元减号。它翻转数字的符号。

``````
public class Main {

public static void main(String[] args) {

int x = 999;

// Change the sign for the first time
x = -x;
System.out.println(x);

// Change the sign for the second time
x= -x;
System.out.println(x);
}
}
``````

• 递增 (++) 和递减 (--)

``````
public class Main {

public static void main(String[] args) {

int x = 999;
x++;
System.out.println(x);

x--;
System.out.println(x);
}
}
``````

``````
public class Main {

public static void main(String[] args) {

int x = 999;
int y = x++;
System.out.println(y);
}
}

``````

``````
public class Main {

public static void main(String[] args) {

int x = 999;
int y = x++;
System.out.println(y);
System.out.println(x);
}
}
``````

``````
public class Main {

public static void main(String[] args) {

int x = 999;
int y = ++x;
System.out.println(y);
}
}
``````

## 复合运算符

• 任务
• 算术运算符

• `+=`
• `-=`
• `*=`
• `/=`
• `%=`

``````
public class Main {

public static void main(String[] args) {

int x = 999;
int y = 33;

x += y;
System.out.println(x);
}
}
``````

## 逻辑运算符

• `!`- 逻辑。它翻转布尔值
``````
public class Main {

public static void main(String[] args) {

boolean x = true;
System.out.println(!x);
}
}
``````

• `&&`- 逻辑。只有当两个操作数都为真时，它才返回真。
``````
public class Main {

public static void main(String[] args) {

System.out.println(100 > 10 && 100 > 200);
System.out.println(100 > 50 && 100 >= 100);
}
}
``````

• `||`- 逻辑当至少一个操作数为真时，它返回真。

``````
public class Main {

public static void main(String[] args) {

System.out.println(100 > 10 || 100 > 200);
}
}
``````

TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION