下面的示例显示算术运算符的用法 - 加法 (+)、减法 (-)、乘法 (*)、除法 (/) 和模 (%) 运算符。
public class MyClass {
public static void main(String[] args) {
float a = 25f;
float b = 10f;
System.out.println("a = "+a+", b = "+b+"\n");
//添加a和b
float result_add = a + b;
System.out.println("a + b = "+result_add);
//a减去b
float result_sub = a - b;
System.out.println("a - b = "+result_sub);
//a和b相乘
float result_mul = a * b;
System.out.println("a * b = "+result_mul);
//a除以b
float result_div = a / b;
System.out.println("a / b = "+result_div);
//返回除法余数
float result_modulo = a % b;
System.out.println("a % b = "+result_modulo);
}
}
输出上面的代码将是:
a = 25.0, b = 10.0
a + b = 35.0
a - b = 15.0
a * b = 250.0
a / b = 2.5
a % b = 5.0