תנאים ו-switch החדשConditions & the new switch
if, else if ו-else, אופרטורים לוגיים, switch עם חצים (->) כביטוי שמחזיר ערך, ו-yield.
if, else if and else, logical operators, switch with arrows (->) as an expression that returns a value, and yield.
זוגי או אי-זוגיEven or odd
תזכורת: מה ביקשוReminder: the task
במשתנה n שימו 17. הדפיסו 17 is odd או 17 is even בעזרת if ו-%.
Put 17 in n. Print 17 is odd or 17 is even using if and %.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
int n = 17;
if (n % 2 == 0) {
System.out.println(n + " is even");
} else {
System.out.println(n + " is odd");
}
}
}17 is odd
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ציון במיליםA grade in words
תזכורת: מה ביקשוReminder: the task
במשתנה score שימו 73. הדפיסו excellent מ-90, good מ-75, pass מ-55, ואחרת fail. (כאן צריך להדפיס pass.)
Put 73 in score. Print excellent from 90, good from 75, pass from 55, otherwise fail. (Here it should print pass.)
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
int score = 73;
if (score >= 90) {
System.out.println("excellent");
} else if (score >= 75) {
System.out.println("good");
} else if (score >= 55) {
System.out.println("pass");
} else {
System.out.println("fail");
}
}
}pass
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
כניסה למתקןRide entry
תזכורת: מה ביקשוReminder: the task
למתקן נכנסים מי שגובהו לפחות 140 וגם גילו לפחות 10, או מי שבא עם הורה. עם height = 135, age = 11, withParent = true, הדפיסו can ride: true.
You may ride if you're at least 140 tall and at least 10 years old, or if you came with a parent. With height = 135, age = 11, withParent = true, print can ride: true.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
int height = 135;
int age = 11;
boolean withParent = true;
boolean canRide = (height >= 140 && age >= 10) || withParent;
System.out.println("can ride: " + canRide);
}
}can ride: true
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
שם היוםThe day's name
תזכורת: מה ביקשוReminder: the task
במשתנה day שימו 3. בעזרת switch עם חצים שמחזיר ערך, שמרו ב-name את שם היום (1 = Sunday … 7 = Saturday, אחרת Unknown) והדפיסו אותו.
Put 3 in day. With an arrow switch that returns a value, store the day's name in name (1 = Sunday … 7 = Saturday, otherwise Unknown) and print it.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
int day = 3;
String name = switch (day) {
case 1 -> "Sunday";
case 2 -> "Monday";
case 3 -> "Tuesday";
case 4 -> "Wednesday";
case 5 -> "Thursday";
case 6 -> "Friday";
case 7 -> "Saturday";
default -> "Unknown";
};
System.out.println(name);
}
}Tuesday
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
צייד באגים: תמיד מבוגרBug hunter: always an adult
תזכורת: מה ביקשוReminder: the task
הקוד אמור להדפיס child לגיל 12, אבל מדפיס גם adult. מצאו את התו המיותר ותקנו.
The code should print child for age 12, but it also prints adult. Find the extra character and fix it.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
int age = 12;
if (age >= 18) {
System.out.println("adult");
}
if (age < 18) {
System.out.println("child");
}
}
}child
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 4: דירוג במיליםShelf, step 4: a rating in words
תזכורת: מה ביקשוReminder: the task
לספר יש int stars (כאן 4). בעזרת switch עם חצים, תרגמו ל-Masterpiece (5), Great (4), Good (3), Meh (1 או 2), ואחרת No rating. הדפיסו Rating: Great. אם stars מחוץ ל-1–5, הדפיסו לפני כן Invalid stars! (השתמשו ב-if).
A book has int stars (here 4). With an arrow switch translate it to Masterpiece (5), Great (4), Good (3), Meh (1 or 2), otherwise No rating. Print Rating: Great. If stars is outside 1–5, first print Invalid stars! (use an if).
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
int stars = 4;
if (stars < 1 || stars > 5) {
System.out.println("Invalid stars!");
}
String word = switch (stars) {
case 5 -> "Masterpiece";
case 4 -> "Great";
case 3 -> "Good";
case 1, 2 -> "Meh";
default -> "No rating";
};
System.out.println("Rating: " + word);
}
}Rating: Great
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
שנה מעוברתLeap year
תזכורת: מה ביקשוReminder: the task
שנה מעוברת מתחלקת ב-4, חוץ משנים שמתחלקות ב-100, אבל כן אם מתחלקת ב-400. בדקו את 1900, 2000 ו-2024 (שלושה משתנים) והדפיסו לכל אחת 1900: false וכן הלאה, עם ביטוי בוליאני אחד שחוזר.
A leap year divides by 4, except years dividing by 100, but yes if it divides by 400. Check 1900, 2000 and 2024 (three variables) and print for each 1900: false and so on, with one boolean expression repeated.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
int y1 = 1900;
int y2 = 2000;
int y3 = 2024;
boolean l1 = (y1 % 4 == 0 && y1 % 100 != 0) || y1 % 400 == 0;
boolean l2 = (y2 % 4 == 0 && y2 % 100 != 0) || y2 % 400 == 0;
boolean l3 = (y3 % 4 == 0 && y3 % 100 != 0) || y3 % 400 == 0;
System.out.println(y1 + ": " + l1);
System.out.println(y2 + ": " + l2);
System.out.println(y3 + ": " + l3);
}
}1900: false 2000: true 2024: true
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
אבן, נייר, מספרייםRock, paper, scissors
תזכורת: מה ביקשוReminder: the task
p1 ו-p2 הם מחרוזות: "rock", "paper" או "scissors". כאן p1 = "rock", p2 = "scissors". הדפיסו Player 1 wins, Player 2 wins או Draw. זכרו: מחרוזות משווים עם equals.
p1 and p2 are strings: "rock", "paper" or "scissors". Here p1 = "rock", p2 = "scissors". Print Player 1 wins, Player 2 wins or Draw. Remember: compare strings with equals.
סימולציה: הפתרון רץ שורה אחרי שורהSimulation: the solution runs line by line
לוחצים הבא או נגן: רואים איזו שורה Java מבצעת, מה יש בכל קופסה (משתנה) ומה מופיע על המסך. זו הרצה אמיתית של הפתרון.
Press Next or Play: see which line Java runs, what is in each box (variable) and what appears on the screen. This is a real run of the solution.
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
הפתרון המלא והפלט שלוFull solution and its output
public class Main {
public static void main(String[] args) {
String p1 = "rock";
String p2 = "scissors";
boolean p1Wins = (p1.equals("rock") && p2.equals("scissors"))
|| (p1.equals("paper") && p2.equals("rock"))
|| (p1.equals("scissors") && p2.equals("paper"));
if (p1.equals(p2)) {
System.out.println("Draw");
} else if (p1Wins) {
System.out.println("Player 1 wins");
} else {
System.out.println("Player 2 wins");
}
}
}Player 1 wins
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.