public class Operators Example public static void mainString



![דוגמה public class Operators. Example { public static void main(String[] args) { int דוגמה public class Operators. Example { public static void main(String[] args) { int](http://slidetodoc.com/presentation_image/30b831c8ae50b9a1544395abf24d0818/image-4.jpg)
דוגמה public class Operators. Example { public static void main(String[] args) { int a = 5; int b = a / 2; double c = 5. 0; double d = c / 2. 0; System. out. println(“b = " + b); System. out. println(“d = " + d); } } ? מה יודפס







אופרטורים יחסיים אופרטורים המשווים בין שני מספרים ונותנים תשובה 6 יש Java ב . (false או true) בוליאנית Operator Name Description x<y Less than true if x is less than y, otherwise false. x>y Greater than true if x is greater than y, otherwise false. x <= y Less than or equal to true if x is less than or equal to y, otherwise false. x >= y Greater than or equal to true if x is greater than or equal to y, otherwise false. x == y Equal true if x equals y, otherwise false. x != y Not Equal true if x is not equal to y, otherwise false. 11

דוגמה import java. util. Scanner; public class Relational. Op { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter the first number: "); int x = sc. next. Int(); System. out. print("Enter the second number: "); int y = sc. next. Int(); boolean cmp. GT = (x > y); boolean cmp. EQ = (x == y); boolean cmp. NEQ = (x != y) ; System. out. println(“x is greater than y? " + cmp. GT); System. out. println(“x is equal to y? " + cmp. EQ); System. out. println(“x is not equal to y? " + cmp. NEQ); System. out. println(“x is less than or equal y? " + (x <= y)); } } 12

אופרטורים לוגיים פועלים על ערכים מטיפוס לוגי )בוליאני( וגם . (false או true) נותנים תשובה בוליאנית Operator Name Description x && y And True if both x and y are true, otherwise false. x || y Or True if at least one of x or y are true, otherwise false. !x Not True if x is false, otherwise false. 13

דוגמה import java. util. Scanner; // This program demonstrates logical operators. // It reads two integers from the user and checks if // they are larger than 10. public class Logical. Operators { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter the first number: "); int x = sc. next. Int(); System. out. print("Enter the second number: "); int y = sc. next. Int(); System. out. println("(y<10)&&(x<10) is " + ((y<10) && (x<10))); System. out. println("(y<10)||(x<10) is " + ((y<10) || (x<10))); boolean state; state = ((y < 10) || (x < 10)); System. out. println("state is " + state); } } Enter the first number: 10 Enter the second number: 9 (y<10)&&(x<10) is false (y<10)||(x<10) is true state is true 14


if - דוגמה ל import java. util. Scanner; public class Triangle { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter the first number: "); double a = sc. next. Int(); System. out. print("Enter the second number: "); double b = sc. next. Int(); System. out. print("Enter the third number: "); double c = sc. next. Int(); if ((a + b <= c) || (a + c <= b) || (b + c <= a)){ System. out. println("There is no triangle with these sides. "); } else{ System. out. println("There is a triangle with these sides. "); } } } 16


פתרון "פשוט" ולא עומד בדרישות import java. util. Scanner; public class Exercise 1{ public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. println("insert 5 numbers"); int a 1 = sc. next. Int(); int a 2 = sc. next. Int(); int a 3 = sc. next. Int(); int a 4 = sc. next. Int(); int a 5 = sc. next. Int(); System. out. println("choose value to search"); int x = sc. next. Int(); boolean found = false; if (x == a 1 || x == a 2 || x == a 3 || x == a 4 || x == a 5) found = true; System. out. println("answer is " + found); } }

![פתרון import java. util. Scanner; public class Exercise 1{ public static void main(String[] פתרון import java. util. Scanner; public class Exercise 1{ public static void main(String[]](http://slidetodoc.com/presentation_image/30b831c8ae50b9a1544395abf24d0818/image-20.jpg)
פתרון import java. util. Scanner; public class Exercise 1{ public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. println("insert 5 numbers"); int a 1 = sc. next. Int(); int a 2 = sc. next. Int(); int a 3 = sc. next. Int(); int a 4 = sc. next. Int(); int a 5 = sc. next. Int(); System. out. println("choose value to search"); int x = sc. next. Int(); boolean found = false; if (x < a 3){ if (x == a 1 || x == a 2){ found = true; } } else if (x == a 3 || x == a 4 || x == a 5){ found = true; } System. out. println("answer is " + found); } }

![פתרון import java. util. Scanner; public class Exercise 2{ public static void main(String[] פתרון import java. util. Scanner; public class Exercise 2{ public static void main(String[]](http://slidetodoc.com/presentation_image/30b831c8ae50b9a1544395abf24d0818/image-22.jpg)
פתרון import java. util. Scanner; public class Exercise 2{ public static void main(String[] args){ Scanner sc = new Scanner(System. in); int a = sc. next. Int(); int b = sc. next. Int(); int c = sc. next. Int(); int d = sc. next. Int(); int min; int second. Min; if (a < b){ min = a; second. Min = b; } else { min = b; second. Min = a; } המשך בעמוד הבא

פתרון if (min > c){ second. Min = min; min = c; } else if (second. Min > c){ second. Min = c; } if (min > d){ second. Min = min; min = d; } else if (second. Min > d){ second. Min = d; } System. out. println("Second minimum is " + second. Min); } }
- Slides: 23