נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
4
חלקPart 1: יסודותFoundations כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

תנאים ו-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.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

1. מה ידפיס הקוד?What does the code print?
main
int x = 15;
if (x > 10) {
    System.out.println("big");
} else if (x > 5) {
    System.out.println("medium");
}

2. מה הערך של 5 > 3 && 2 > 4?What is 5 > 3 && 2 > 4?

3. מה ההבדל העיקרי של case 1 -> ... מ-case 1: ...?What's the main difference between case 1 -> ... and case 1: ...?

תרגיליםExercises

תרגילExercise 1

זוגי או אי-זוגיEven or odd

במשתנה n שימו 17. הדפיסו 17 is odd או 17 is even בעזרת if ו-%.

Put 17 in n. Print 17 is odd or 17 is even using if and %.

איך הפלט אמור להיראות?What should the output look like?
17 is odd
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: מספר זוגי אם השארית מחלוקה ב-2 היא 0: n % 2 == 0.A number is even if dividing by 2 leaves 0: n % 2 == 0.
לפתרוןSolution
תרגילExercise 2

ציון במיליםA grade in words

במשתנה 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.)

איך הפלט אמור להיראות?What should the output look like?
pass
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: מתחילים מהתנאי הגבוה: if (score >= 90).Start with the highest condition: if (score >= 90).
רמזHint 2: ואז else if (score >= 75) וכן הלאה, ובסוף else.Then else if (score >= 75) and so on, and else at the end.
לפתרוןSolution
תרגילExercise 3

כניסה למתקןRide entry

למתקן נכנסים מי שגובהו לפחות 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.

איך הפלט אמור להיראות?What should the output look like?
can ride: true
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: (height >= 140 && age >= 10) || withParent(height >= 140 && age >= 10) || withParent
רמזHint 2: שמרו את התוצאה במשתנה boolean.Keep the result in a boolean variable.
לפתרוןSolution
תרגילExercise 4

שם היוםThe day's name

במשתנה 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.

איך הפלט אמור להיראות?What should the output look like?
Tuesday
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: String name = switch (day) { case 1 -> "Sunday"; ... default -> "Unknown"; };String name = switch (day) { case 1 -> "Sunday"; ... default -> "Unknown"; };
רמזHint 2: שימו לב לנקודה-פסיק אחרי הסוגר } של ה-switch.Mind the semicolon after the switch's closing }.
לפתרוןSolution
תרגילExercise 5

צייד באגים: תמיד מבוגרBug hunter: always an adult

הקוד אמור להדפיס child לגיל 12, אבל מדפיס גם adult. מצאו את התו המיותר ותקנו.

The code should print child for age 12, but it also prints adult. Find the extra character and fix it.

איך הפלט אמור להיראות?What should the output look like?
child
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: הסתכלו מאוד בזהירות על סוף השורה של ה-if הראשון.Look very carefully at the end of the first if line.
רמזHint 2: ; אחרי if (...) הוא "משפט ריק": ה-if לא שולט בבלוק שאחריו, והבלוק רץ תמיד.A ; after if (...) is an "empty statement": the if doesn't control the block after it, and the block always runs.
לפתרוןSolution
תרגילExercise 6

מדף, צעד 4: דירוג במיליםShelf, step 4: a rating in words

פרויקט מלווהCompanion project

לספר יש 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).

איך הפלט אמור להיראות?What should the output look like?
Rating: Great
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: if (stars < 1 || stars > 5)if (stars < 1 || stars > 5)
רמזHint 2: case 1, 2 -> "Meh";case 1, 2 -> "Meh";
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

שנה מעוברתLeap year

שנה מעוברת מתחלקת ב-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.

איך הפלט אמור להיראות?What should the output look like?
1900: false
2000: true
2024: true
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: (y % 4 == 0 && y % 100 != 0) || y % 400 == 0(y % 4 == 0 && y % 100 != 0) || y % 400 == 0
לפתרוןSolution
אתגרChallenge 2

אבן, נייר, מספרייםRock, paper, scissors

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.

איך הפלט אמור להיראות?What should the output look like?
Player 1 wins
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: תיקו: p1.equals(p2).Draw: p1.equals(p2).
רמזHint 2: שחקן 1 מנצח אם (rock ו-scissors) או (paper ו-rock) או (scissors ו-paper).Player 1 wins on (rock and scissors) or (paper and rock) or (scissors and paper).
לפתרוןSolution

נבנה על ידיBuilt by Ariel Yohimovich