Java Tutorial/Operators/Arithmetic Operators
Содержание
Applying the modulus operator, %, to floating-point values
public class MainClass{
public static void main(String[] arg){
double a = 2.4;
double b = 0.2;
System.out.println( a % b );
}
}
0.1999999999999998
Arithmetic Calculations
An assignment statement has three elements:
- the variable to store the result,
- the assignment operator: =,
- an arithmetic expression
The statement is terminated by a semicolon.
public class MainClass{
public static void main(String[] argv){
int a = 1;
int b = 2;
int c = 0;
c = a + b;
System.out.println(c);
}
}
3
Arithmetic Operators
OperatorResult+Addition-Subtraction (also unary minus)*Multiplication/Division%Modulus++Increment+=Addition assignment-=Subtraction assignment*=Multiplication assignment/=Division assignment%=Modulus assignment--Decrement
Demonstrates the mathematical operators
import java.util.Random;
public class MainClass {
static void printInt(String s, int i) {
System.out.println(s + " = " + i);
}
static void printFloat(String s, float f) {
System.out.println(s + " = " + f);
}
public static void main(String[] args) {
Random rand = new Random();
int i, j, k;
j = rand.nextInt(100) + 1;
k = rand.nextInt(100) + 1;
printInt("j", j);
printInt("k", k);
i = j + k;
printInt("j + k", i);
i = j - k;
printInt("j - k", i);
i = k / j;
printInt("k / j", i);
i = k * j;
printInt("k * j", i);
i = k % j;
printInt("k % j", i);
j %= k;
printInt("j %= k", j);
// Floating-point number tests:
float u, v, w; // applies to doubles, too
v = rand.nextFloat();
w = rand.nextFloat();
printFloat("v", v);
printFloat("w", w);
u = v + w;
printFloat("v + w", u);
u = v - w;
printFloat("v - w", u);
u = v * w;
printFloat("v * w", u);
u = v / w;
printFloat("v / w", u);
// the following also works for
// char, byte, short, int, long,
// and double:
u += v;
printFloat("u += v", u);
u -= v;
printFloat("u -= v", u);
u *= v;
printFloat("u *= v", u);
u /= v;
printFloat("u /= v", u);
}
}
j = 31 k = 84 j + k = 115 j - k = -53 k / j = 2 k * j = 2604 k % j = 22 j %= k = 31 v = 0.79780066 w = 0.9309381 v + w = 1.7287388 v - w = -0.13313746 v * w = 0.7427031 v / w = 0.8569857 u += v = 1.6547863 u -= v = 0.8569857 u *= v = 0.6837037 u /= v = 0.8569856
Modulus operator %: obtain the remainder after a division
public class MainClass {
public static void main(String[] argv) {
int a = 3 % 2;
int b = 11 % 3;
int c = 7 % -3;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
1 2 1
The Basic Arithmetic Operators
public class MainClass {
public static void main(String args[]) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
Integer Arithmetic a = 2 b = 6 c = 1 d = -1 e = 1 Floating Point Arithmetic da = 2.0 db = 6.0 dc = 1.5 dd = -0.5 de = 0.5