Hello java This program prints Hello World on

  • Slides: 34
Download presentation

 תוכנית ראשונה לסימני לב שימו. Hello. java בקובץ הבאה התוכנית את הקלידו .

תוכנית ראשונה לסימני לב שימו. Hello. java בקובץ הבאה התוכנית את הקלידו . וקטנות גדולות ואותיות , פיסוק /* This program prints "Hello World!" on the screen */ public class Hello { public static void main(String[] args) { // print “Hello World!” on the screen System. out. println("Hello World!"); } } 4

 משתנים • טבלת הגדרה • שמות • ( הכרה )טווח סקופ • public

משתנים • טבלת הגדרה • שמות • ( הכרה )טווח סקופ • public class Example 1 { • השמה public static void main(String[] args){ • פלט : דוגמה int num; num = 3; int num 1 = 1, num 2; num 2 = num 1; System. out. println("num = " + num); System. out. println("num 1 = " + num 1); System. out. println("num 2 = " + num 2); } } scope of num 1 and num 2 9

 טבלת סיכום טיפוסים מספריים 13 טיפוס מספר ה bytes ערכים byte short int

טבלת סיכום טיפוסים מספריים 13 טיפוס מספר ה bytes ערכים byte short int long float 1 2 4 8 4 -27 עד -172 -215 עד -1152 -231 עד -1312 -263 עד -1632 double 8

1 דוגמה /* This program demonstrates addition and multiplication between integers. */ public class

1 דוגמה /* This program demonstrates addition and multiplication between integers. */ public class Operators. Example 1{ public static void main(String[] args){ int a = 3, b = 5; int c; c = a + b; System. out. println("c = “ + c); c = c * 2; System. out. println("c = “ + c); } } c=8 c = 16 16

2 דוגמה : האריתמטיות הפעולות של הפעולות סדר את מדגימה הבאה התוכנית public class

2 דוגמה : האריתמטיות הפעולות של הפעולות סדר את מדגימה הבאה התוכנית public class Operators. Example { public static void main(String[] args) { int a = 18; int b = 42; int first = (a + b) * 2; int second = a + b * 2; System. out. println("first number is: " + first); System. out. println("second number is: " + second); } } first number is: 120 second number is: 102 17

3 דוגמה import java. util. Scanner; /* This program accepts a 3 -digit number

3 דוגמה import java. util. Scanner; /* This program accepts a 3 -digit number from the user * reverses the digits and prints the result */ public class Reverse { public static void main(String[] args) { // Read a number from the user. Scanner sc = new Scanner(System. in); System. out. print("Enter a 3 -digit number: "); int num = sc. next. Int(); // divide the number into ones, tens and hundreds. int ones = num % 10; int tens = (num % 100) / 10; int hundreds = num / 100; // calculate the reverse number int reverse. Num = (ones * 100) + (tens * 10) + hundreds; System. out. println("The reverse number is " + reverse. Num); } } Enter a 3 -digit number: 951 The reverse number is 159 18

 אופרטורים יחסיים תשובה ונותנים מספרים שני בין המשווים אופרטורים 6 יש Java ב

אופרטורים יחסיים תשובה ונותנים מספרים שני בין המשווים אופרטורים 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. 19

 דוגמה import java. util. Scanner; // This program compares two numbers with relational

דוגמה import java. util. Scanner; // This program compares two numbers with relational operators public class Relational. 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("x<y is " + (x < y)); System. out. println("x>y is " + (x > y)); System. out. println("x<=y is " + (x <= y)); System. out. println("x>=y is " + (x >= y)); System. out. println("x==y is " + (x == y)); System. out. println("x!=y is " + (x != y)); } } Enter the first number: 10 Enter the second number: 20 x<y is true x>y is false x<=y is true x>=y is false x==y is false x!=y is true 20

 אופרטורים לוגיים לוגי )בוליאני( וגם מטיפוס ערכים על פועלים לוגיים אופרטורים . (false

אופרטורים לוגיים לוגי )בוליאני( וגם מטיפוס ערכים על פועלים לוגיים אופרטורים . (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. . לימין משמאל מתבצעת Or ו And הלוגיים האופרטורים הערכת * 21

 דוגמה import java. util. Scanner; // This program demonstrates logical operators. // It

דוגמה 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 22

 דוגמה public class Use. Math { public static void main(String[] args){ // This

דוגמה public class Use. Math { public static void main(String[] args){ // This is an example of using Math methods double x = Math. abs(-3); x = Math. pow(x, 2 ); x = Math. max(x, Math. PI); System. out. println("max( (|-3|)^2 , Pi ) = " + x); x = Math. random(); System. out. println("A random number between 0 and 1: "+ x); } } max( (|-3|)^2 , Pi ) = 9. 0 A random number between 0 and 1: 0. 9764623048094814 הכלולות Java בספריות כלולות נוספות רבות ומחלקות Math המחלקה . Java בהתקנת ניתן Java ב הכלולות נוספות ומחלקות Math מחלקת על מפורט מידע בקישור למצוא http: //docs. oracle. com/javase/7/docs/api/java/lang/Math. html 24

if-else - דוגמה ל import java. util. Scanner; public class Pythagorean { public static

if-else - דוגמה ל import java. util. Scanner; public class Pythagorean { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter the first side: "); double a = sc. next. Int(); System. out. print("Enter the second side: "); double b = sc. next. Int(); System. out. print("Enter the third side: "); double c = sc. next. Int(); if (a*a + b*b == c*c){ System. out. println("The triplet is a pythagorean triple"); } else{ System. out. println("The triplet is not a pythagorean triple"); } } } 30

 דוגמה import java. util. Scanner; // compute n! using while and for loops.

דוגמה import java. util. Scanner; // compute n! using while and for loops. public class Factorial { public static void main(String[] args) { Scanner sc = new Scanner(System. in); System. out. print("Enter a non-negative number: "); int n = sc. next. Int(); // 1. using while int fact = 1; int i = 1; while ( i <= n ) { fact = fact * i; i = i + 1; } System. out. println("n!=" + fact); // 2. using for fact = 1; for ( i = 1; i <= n; i = i + 1){ fact = fact * i; } System. out. println("n!=" + fact); } } 32