【Java探索之旅】运算符解析 算术运算符,关系运算符
在Java中,算术运算符主要用于进行基本数值的算术运算,关系运算符用于比较两个值之间的关系。以下是一些常见的算术运算符和关系运算符的示例代码:
public class OperatorsExample {
public static void main(String[] args) {
// 算术运算符示例
int a = 10;
int b = 5;
int sum = a + b; // 加法
int difference = a - b; // 减法
int product = a * b; // 乘法
int quotient = a / b; // 整数除法
int remainder = a % b; // 取余
// 关系运算符示例
boolean isEqual = (a == b); // 等于
boolean isNotEqual = (a != b); // 不等于
boolean isGreater = (a > b); // 大于
boolean isLess = (a < b); // 小于
boolean isGreaterOrEqual = (a >= b); // 大于等于
boolean isLessOrEqual = (a <= b); // 小于等于
// 输出结果
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
System.out.println("Is a equal to b? " + isEqual);
System.out.println("Is a not equal to b? " + isNotEqual);
System.out.println("Is a greater than b? " + isGreater);
System.out.println("Is a less than b? " + isLess);
System.out.println("Is a greater than or equal to b? " + isGreaterOrEqual);
System.out.println("Is a less than or equal to b? " + isLessOrEqual);
}
}
这段代码演示了如何使用Java中的算术运算符和关系运算符。算术运算符用于基本算术操作,关系运算符用于比较两个值。在输出中,你将看到各种运算的结果。
评论已关闭