for, while, do-while ו-for-each, break ו-continue, לולאות מקוננות ומעקב אחרי משתני לולאה.
for, while, do-while and for-each, break and continue, nested loops and tracing loop variables.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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
תרגיליםExercises
תרגילExercise 1
ספירה בשורה אחתCounting on one line
הדפיסו בשורה אחת את המספרים 1 עד 10 עם רווח אחרי כל מספר, ואחר כך done בשורה חדשה.
Print the numbers 1 to 10 on one line with a space after each, then done on a new line.
איך הפלט אמור להיראות?What should the output look like?
1 2 3 4 5 6 7 8 9 10
done
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:for (int i = 1; i <= 10; i++)for (int i = 1; i <= 10; i++)
רמזHint 2:System.out.print(i + " "); כדי להישאר באותה שורה.System.out.print(i + " "); to stay on the same line.
איך הפלט אמור להיראות?What should the output look like?
Sum: 55
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:int sum = 0; לפני הלולאה.int sum = 0; before the loop.
רמזHint 2:בתוך הלולאה: sum += i;.Inside the loop: sum += i;.
עם while, הדפיסו 5, 4, 3, 2, 1 (כל מספר בשורה) ואחר כך Liftoff!.
With while, print 5, 4, 3, 2, 1 (one per line) and then Liftoff!.
איך הפלט אמור להיראות?What should the output look like?
5
4
3
2
1
Liftoff!
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:int n = 5; while (n > 0) { ... n--; }int n = 5; while (n > 0) { ... n--; }
רמזHint 2:בלי n-- הלולאה לא תיגמר!Without n-- the loop never ends!
איך הפלט אמור להיראות?What should the output look like?
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
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:System.out.println("7 x " + i + " = " + (7 * i));System.out.println("7 x " + i + " = " + (7 * i));
כמה תנועות (a, e, i, o, u) יש במילה "programming"? עברו על התווים עם for-each והדפיסו Vowels: 3.
How many vowels (a, e, i, o, u) are in "programming"? Go over the characters with for-each and print Vowels: 3.
איך הפלט אמור להיראות?What should the output look like?
Vowels: 3
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:for (char c : word.toCharArray())for (char c : word.toCharArray())
רמזHint 2:if ("aeiou".indexOf(c) >= 0) אומר ש-c הוא תנועה.if ("aeiou".indexOf(c) >= 0) means c is a vowel.
The program should print 1 to 5, but stops at 4. Fix the off-by-one.
איך הפלט אמור להיראות?What should the output look like?
1
2
3
4
5
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:מה קורה כש-i שווה 5? התנאי i < 5 הוא false.What happens when i is 5? i < 5 is false.
מדף, צעד 5: רשימה ממוספרתShelf, step 5: a numbered list
פרויקט מלווהCompanion project
במחרוזת books יש שמות של 3 ספרים מופרדים ב-;: "Dune;Emma;Ulysses". עברו עליהם (books.split(";") נותן אותם אחד-אחד; מערכים נלמד במפגש 8) והדפיסו רשימה ממוספרת 1. Dune וכן הלאה, ובסוף 3 books.
books holds 3 titles separated by ;: "Dune;Emma;Ulysses". Go over them (books.split(";") hands them out one by one; arrays come in session 8) and print a numbered list 1. Dune and so on, then 3 books at the end.
איך הפלט אמור להיראות?What should the output look like?
1. Dune
2. Emma
3. Ulysses
3 books
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:for (String title : books.split(";"))for (String title : books.split(";"))
רמזHint 2:מונה משלכם: int n = 0; לפני, ו-n++ בכל סיבוב.Your own counter: int n = 0; before, and n++ each round.
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.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:בודקים קודם את שניהם: i % 15 == 0.Check both first: i % 15 == 0.
רמזHint 2:אחר כך % 3, אחר כך % 5, ואחרת המספר.Then % 3, then % 5, otherwise the number.
עם לולאות מקוננות הדפיסו 4 שורות: 1, 12, 123, 1234.
With nested loops print 4 lines: 1, 12, 123, 1234.
איך הפלט אמור להיראות?What should the output look like?
1
12
123
1234
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:החיצונית בוחרת שורה (row מ-1 עד 4), הפנימית מדפיסה 1 עד row.The outer loop picks a row (row 1 to 4); the inner prints 1 to row.
רמזHint 2:אחרי הלולאה הפנימית: System.out.println();.After the inner loop: System.out.println();.