מערכים ומערכים דו-ממדייםArrays & 2D arrays
יצירה, גישה ואורך, לולאות על מערכים, Arrays.toString ו-sort, ומערכים דו-ממדיים כמו לוח משחק.
Creating, accessing and length, looping over arrays, Arrays.toString and sort, and 2D arrays like a game board.
ממוצע ציוניםAverage of grades
תזכורת: מה ביקשוReminder: the task
במערך {85, 92, 78, 95} חשבו בלולאה את הממוצע והדפיסו Average: 87.5.
For the array {85, 92, 78, 95} compute the average in a loop and print Average: 87.5.
סימולציה: הפתרון רץ שורה אחרי שורה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[] grades = {85, 92, 78, 95};
int sum = 0;
for (int g : grades) {
sum += g;
}
System.out.println("Average: " + (double) sum / grades.length);
}
}Average: 87.5
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
הכי חםThe hottest
תזכורת: מה ביקשוReminder: the task
במערך טמפרטורות {24, 31, 28, 35, 30} מצאו את הגבוהה ואת היום שלה (אינדקס מ-0), והדפיסו Max 35 on day 3.
In the temperature array {24, 31, 28, 35, 30} find the highest and its day (index from 0), and print Max 35 on day 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) {
int[] temps = {24, 31, 28, 35, 30};
int max = temps[0];
int day = 0;
for (int i = 1; i < temps.length; i++) {
if (temps[i] > max) {
max = temps[i];
day = i;
}
}
System.out.println("Max " + max + " on day " + day);
}
}Max 35 on day 3
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
הופכים מערךReversing an array
תזכורת: מה ביקשוReminder: the task
כתבו static void reverse(int[] a) שהופכת את המערך במקום (מחליפה את הראשון באחרון וכן הלאה). הדפיסו את {1, 2, 3, 4, 5} אחרי ההיפוך עם Arrays.toString.
Write static void reverse(int[] a) that reverses the array in place (swap first and last, and so on). Print {1, 2, 3, 4, 5} after reversing, with Arrays.toString.
סימולציה: הפתרון רץ שורה אחרי שורה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
import java.util.Arrays;
public class Main {
static void reverse(int[] a) {
for (int i = 0; i < a.length / 2; i++) {
int t = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = t;
}
}
public static void main(String[] args) {
int[] n = {1, 2, 3, 4, 5};
reverse(n);
System.out.println(Arrays.toString(n));
}
}[5, 4, 3, 2, 1]
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
צייד באגים: מעבר לקצהBug hunter: past the edge
תזכורת: מה ביקשוReminder: the task
התוכנית קורסת עם ArrayIndexOutOfBoundsException. צפו בסימולציה "איפה זה נשבר" בעמוד הפתרונות, ותקנו כך שתדפיס 60.
The program crashes with ArrayIndexOutOfBoundsException. Watch the "where it breaks" simulation on the solutions page, and fix it so it prints 60.
סימולציה: הפתרון רץ שורה אחרי שורה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 where it breaks
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
צפו בהרצה צעד אחר צעד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[] a = {10, 20, 30};
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
System.out.println(sum);
}
}60
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
לוח איקס-עיגולA tic-tac-toe board
תזכורת: מה ביקשוReminder: the task
צרו char[][] board = new char[3][3] ומלאו בנקודות (.). שימו X באמצע ו-O בפינה הימנית העליונה (board[0][2]), והדפיסו את הלוח (3 שורות כמו ..O).
Create char[][] board = new char[3][3] and fill it with dots (.). Put X in the middle and O in the top-right corner (board[0][2]), and print the board (3 lines like ..O).
סימולציה: הפתרון רץ שורה אחרי שורה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) {
char[][] board = new char[3][3];
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
board[r][c] = '.';
}
}
board[1][1] = 'X';
board[0][2] = 'O';
for (char[] row : board) {
System.out.println(new String(row));
}
}
}..O .X. ...
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 8: מערך של ספריםShelf, step 8: an array of books
תזכורת: מה ביקשוReminder: the task
מערך String[] titles = {"Emma", "Dune", "Ulysses", "Beloved"} ומערך int[] years = {1815, 1965, 1922, 1987}. הדפיסו את הספר הישן ביותר (Oldest: Emma (1815)), ואת השמות ממוינים לפי א-ב (Arrays.sort על עותק).
An array String[] titles = {"Emma", "Dune", "Ulysses", "Beloved"} and int[] years = {1815, 1965, 1922, 1987}. Print the oldest book (Oldest: Emma (1815)), and the titles sorted alphabetically (Arrays.sort on a copy).
סימולציה: הפתרון רץ שורה אחרי שורה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
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] titles = {"Emma", "Dune", "Ulysses", "Beloved"};
int[] years = {1815, 1965, 1922, 1987};
int oldest = 0;
for (int i = 1; i < years.length; i++) {
if (years[i] < years[oldest]) {
oldest = i;
}
}
System.out.println("Oldest: " + titles[oldest] + " (" + years[oldest] + ")");
String[] sorted = titles.clone();
Arrays.sort(sorted);
System.out.println(Arrays.toString(sorted));
}
}Oldest: Emma (1815) [Beloved, Dune, Emma, Ulysses]
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ספירת הופעותCounting occurrences
תזכורת: מה ביקשוReminder: the task
קוביות הוטלו: {3, 6, 1, 3, 3, 6, 2}. בעזרת מערך מונים int[] count = new int[7] (תא לכל תוצאה), הדפיסו לכל מספר שהופיע שורה כמו 3: 3 times, לפי הסדר.
Dice rolls: {3, 6, 1, 3, 3, 6, 2}. With a counter array int[] count = new int[7] (a cell per result), print a line for every number that came up, like 3: 3 times, in order.
סימולציה: הפתרון רץ שורה אחרי שורה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[] rolls = {3, 6, 1, 3, 3, 6, 2};
int[] count = new int[7];
for (int r : rolls) {
count[r]++;
}
for (int i = 1; i <= 6; i++) {
if (count[i] > 0) {
System.out.println(i + ": " + count[i] + " times");
}
}
}
}1: 1 times 2: 1 times 3: 3 times 6: 2 times
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
הזזה מעגליתRotate
תזכורת: מה ביקשוReminder: the task
כתבו static int[] rotate(int[] a) שמחזירה מערך חדש שבו כל איבר זז מקום אחד ימינה, והאחרון עובר להתחלה: {1, 2, 3, 4} נהיה {4, 1, 2, 3}. המקורי לא משתנה. הדפיסו את שניהם.
Write static int[] rotate(int[] a) returning a new array where every item moves one place right and the last goes to the start: {1, 2, 3, 4} becomes {4, 1, 2, 3}. The original doesn't change. Print 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
import java.util.Arrays;
public class Main {
static int[] rotate(int[] a) {
int[] r = new int[a.length];
for (int i = 0; i < a.length; i++) {
r[(i + 1) % a.length] = a[i];
}
return r;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4};
int[] b = rotate(a);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
}
}[1, 2, 3, 4] [4, 1, 2, 3]
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.