在Java中可以通過使用四則運算符來實現加、減、乘、除操作。以下是一些示例代碼:
int a = 5;
int b = 3;
int sum = a + b;
System.out.println("Sum: " + sum);
int a = 5;
int b = 3;
int difference = a - b;
System.out.println("Difference: " + difference);
int a = 5;
int b = 3;
int product = a * b;
System.out.println("Product: " + product);
int a = 6;
int b = 3;
int quotient = a / b;
System.out.println("Quotient: " + quotient);
需要注意的是,在進行除法操作時,要確保被除數和除數都是整數類型,否則會導致結果不準確。如果需要進行浮點數的除法操作,可以將被除數或除數改為浮點型,例如:
double a = 6.0;
int b = 3;
double quotient = a / b;
System.out.println("Quotient: " + quotient);