Java använder flera olika operatorer. Här anges några exempel:
import java.util.Scanner;
public class AritmeticOperators {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Ange första heltal: ");
int tal1 = input.nextInt();
System.out.print("Ange andra heltal: ");
int tal2 = input.nextInt();
if (tal1 > tal2) {
System.out.println("Additionen " + tal1 + " + " + tal2 + " = " + (tal1 + tal2));
System.out.println("Subtraktionen " + tal1 + " - " + tal2 + " = " + (tal1 - tal2));
System.out.println("Multiplikationen " + tal1 + " * " + tal2 + " = " + (tal1 * tal2));
System.out.println("Divisionen " + tal1 + " / " + tal2 + " = " + (tal1 / tal2));
System.out.println("Modulus " + tal1 + " % " + tal2 + " = " + (tal1 % tal2));
} else {
System.out.println("Första heltal ska vara större en den andra heltal.");
System.out.print("Ange första heltal: ");
int tal3 = input.nextInt();
System.out.print("Ange andra heltal: ");
int tal4 = input.nextInt();
System.out.println("Subtraktionen " + tal3 + " - " + tal4 + " = " + (tal3 - tal4));
}
}
}
import java.util.Scanner;
public class TilldelandeOperatorer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Ange första heltal: ");
int tal1 = input.nextInt();
System.out.print("Ange andra heltal: ");
int tal2 = input.nextInt();
if (tal1 > tal2) {
int tal3 = tal1 += tal2;
int tal4 = tal1 -= tal2;
int tal5 = tal1 *= tal2;
int tal6 = tal1 /= tal2;
double tal7 = tal1 %= tal2;
System.out.println("Additionen tal1 += tal2 ger tal1 = " + tal3);
System.out.println("Subtraktionen tal1 -= tal2 ger tal1 = " + tal4);
System.out.println("Multiplikationen tal1 *= tal2 ger tal1 " + tal5);
System.out.println("Divisionen tal1 /= tal2 ger tal1 " + tal6);
System.out.println("Modulus tal1 %= tal2 ger tal1 = " + tal7);
} else {
System.out.println("Första heltal ska vara större en den andra heltal.");
System.out.print("Ange första heltal: ");
int tal8 = input.nextInt();
System.out.print("Ange andra heltal: ");
int tal9 = input.nextInt();
int tal10 = tal1 += tal2;
int tal11 = tal1 -= tal2;
int tal12 = tal1 *= tal2;
int tal13 = tal1 /= tal2;
double tal14 = tal1 %= tal2;
System.out.println("Additionen tal1 += tal2 ger tal1 = " + tal10);
System.out.println("Subtraktionen tal1 -= tal2 ger tal1 = " + tal11);
System.out.println("Multiplikationen tal1 *= tal2 ger tal1 " + tal12);
System.out.println("Divisionen tal1 /= tal2 ger tal1 " + tal13);
System.out.println("Modulus tal1 %= tal2 ger tal1 = " + tal14);
}
}
}
import java.util.Scanner;
public class PostfixPrefixOperatorer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Ange variabelns värde a = ");
int a = input.nextInt();
System.out.print("Ange variabelns värde b = ");
int b = input.nextInt();
System.out.println("Variabel a = " + a + " och variabel b = " + b);
System.out.println("Prefix ++a ger att a = " + (++a) + " och prefix ++b ger b = " + (++b));
System.out.println("Variablerna är nu a = " + a + " och b = " + b);
System.out.println("Postfix a++ ger att a = " + (a++) + " och b++ ger b = " + (b++));
System.out.println("Ingen förändring, det vill säga att variablerna ändras efter den här raden.");
System.out.println("Variabel a = " + a + " och variabel b = " + b);
}
}
import java.util.Scanner;
public class RelationsOperatorer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Ange variabelns värde a = ");
int a = input.nextInt();
System.out.print("Ange variabelns värde b = ");
int b = input.nextInt();
System.out.println("Observera att relationsoperatorer anger som svar antingen true eller false ");
System.out.println("a < b ger " + (a < b));
System.out.println("a > b ger " + (a > b));
System.out.println("a <= b ger " + (a <= b));
System.out.println("a >= b ger " + (a >= b));
System.out.println("a == b ger " + (a == b));
System.out.println("a != b ger " + (a != b));
}
}
import java.util.Scanner;
public class LogiskaOperatorer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Ange variabelns värde a = ");
int a = input.nextInt();
System.out.print("Ange variabelns värde b = ");
int b = input.nextInt();
System.out.print("Ange variabelns värde c = ");
int c = input.nextInt();
System.out.print("Ange variabelns värde d = ");
int d = input.nextInt();
System.out.println("Observera att logiska operatorer anger som svar antingen true eller false ");
System.out.println("a < b och c < d ger " + (a < b && c < d) + " eftersom " + a + " < " + b
+ " och " + c + " < " + d);
System.out.println("a < b eller c < d ger " + (a < b || c < d) + " eftersom " + a + " < " + b
+ " eller " + c + " < " + d);
System.out.println("a <= b och c <= d ger " + (a <= b && c <= d) + " eftersom " + a +
" <= " + b + " och " + c + " <= " + d);
System.out.println("a <= b eller c <= d ger " + (a <= b || c <= d) + " eftersom " + a +
" <= " + b + " eller " + c + " <= " + d);
System.out.println("a >= b och c >= d ger " + (a >= b && c >= d) + " eftersom " + a +
" >= " + b + " och " + c + " >= " + d);
System.out.println("a == b eller c == d ger " + (a == b || c == d) + " eftersom " + a + " == " + b
+ " eller " + c + " == " + d);
System.out.println("a == b och c != d ger " + (a == b && c != d) + " eftersom " + a + " == " + b
+ " och " + c + " != " + d);
System.out.println("a != b eller c != d ger " + (a != b || c != d) + " eftersom " + a + " != " + b
+ " eller " + c + " != " + d);
}
}