לולאותLoops
for, while, do-while ו-for-each, break ו-continue, לולאות מקוננות ומעקב אחרי משתני לולאה.
for, while, do-while and for-each, break and continue, nested loops and tracing loop variables.
ספירה בשורה אחתCounting on one line
תזכורת: מה ביקשוReminder: the task
הדפיסו בשורה אחת את המספרים 1 עד 10 עם רווח אחרי כל מספר, ואחר כך done בשורה חדשה.
Print the numbers 1 to 10 on one line with a space after each, then done on a new line.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}
System.out.println();
System.out.println("done");
}
}1 2 3 4 5 6 7 8 9 10 done
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
סכום 1 עד 10Sum of 1 to 10
תזכורת: מה ביקשוReminder: the task
חשבו בלולאה את הסכום 1+2+...+10 והדפיסו Sum: 55.
Compute 1+2+...+10 in a loop and print Sum: 55.
סימולציה: הפתרון רץ שורה אחרי שורה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 sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
}
}Sum: 55
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ספירה לאחורCountdown
תזכורת: מה ביקשוReminder: the task
עם while, הדפיסו 5, 4, 3, 2, 1 (כל מספר בשורה) ואחר כך Liftoff!.
With while, print 5, 4, 3, 2, 1 (one per line) and then Liftoff!.
סימולציה: הפתרון רץ שורה אחרי שורה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 = 5;
while (n > 0) {
System.out.println(n);
n--;
}
System.out.println("Liftoff!");
}
}5 4 3 2 1 Liftoff!
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
לוח הכפל של 7The 7 times table
תזכורת: מה ביקשוReminder: the task
הדפיסו 5 שורות: 7 x 1 = 7 עד 7 x 5 = 35.
Print 5 lines: 7 x 1 = 7 to 7 x 5 = 35.
סימולציה: הפתרון רץ שורה אחרי שורה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 = 7;
for (int i = 1; i <= 5; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}
}7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
סופרים תנועותCounting vowels
תזכורת: מה ביקשוReminder: the task
כמה תנועות (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.
סימולציה: הפתרון רץ שורה אחרי שורה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 word = "programming";
int count = 0;
for (char c : word.toCharArray()) {
if ("aeiou".indexOf(c) >= 0) {
count++;
}
}
System.out.println("Vowels: " + count);
}
}Vowels: 3
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
צייד באגים: חסר אחדBug hunter: one missing
תזכורת: מה ביקשוReminder: the task
התוכנית אמורה להדפיס את המספרים 1 עד 5, אבל מדפיסה רק עד 4. תקנו את ה-טעות של אחד (off-by-one).
The program should print 1 to 5, but stops at 4. Fix the off-by-one.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
}
}1 2 3 4 5
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 5: רשימה ממוספרתShelf, step 5: a numbered list
תזכורת: מה ביקשוReminder: the task
במחרוזת 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.
סימולציה: הפתרון רץ שורה אחרי שורה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 books = "Dune;Emma;Ulysses";
int n = 0;
for (String title : books.split(";")) {
n++;
System.out.println(n + ". " + title);
}
System.out.println(n + " books");
}
}1. Dune 2. Emma 3. Ulysses 3 books
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
FizzBuzzFizzBuzz
תזכורת: מה ביקשוReminder: the task
הדפיסו את 1 עד 15, אבל במקום מספר שמתחלק ב-3 כתבו Fizz, במקום מתחלק ב-5 Buzz, ובמקום מתחלק בשניהם FizzBuzz.
Print 1 to 15, but write Fizz for numbers divisible by 3, Buzz for divisible by 5, and FizzBuzz for both.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
for (int i = 1; i <= 15; i++) {
if (i % 15 == 0) {
System.out.println("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
}
}1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
משולש מספריםA number triangle
תזכורת: מה ביקשוReminder: the task
עם לולאות מקוננות הדפיסו 4 שורות: 1, 12, 123, 1234.
With nested loops print 4 lines: 1, 12, 123, 1234.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(col);
}
System.out.println();
}
}
}1 12 123 1234
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.