דיבוג ב-IntelliJDebugging in IntelliJ
נקודות עצירה, Step Over ו-Step Into, צפייה במשתנים, קריאת stack trace ושיטה למציאת באגים.
Breakpoints, Step Over and Step Into, watching variables, reading a stack trace and a method for finding bugs.
ממוצע שיוצא עגול מדיAn average that's too round
תזכורת: מה ביקשוReminder: the task
הממוצע של 2 ו-3 יוצא 2.0. מצאו למה ותקנו כך שיודפס 2.5.
The average of 2 and 3 comes out 2.0. Find out why and fix it so it prints 2.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 a = 2;
int b = 3;
double avg = (a + b) / 2.0;
System.out.println(avg);
}
}2.5
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
האות האחרונהThe last letter
תזכורת: מה ביקשוReminder: the task
last("Java") אמורה להחזיר a, והתוכנית קורסת. קראו את ה-stack trace, מצאו את השורה ותקנו.
last("Java") should return a, and the program crashes. Read the stack trace, find the line 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 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 {
static char last(String s) {
return s.charAt(s.length() - 1);
}
public static void main(String[] args) {
System.out.println(last("Java"));
}
}a
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
תנועה חסרהA missing vowel
תזכורת: מה ביקשוReminder: the task
ב-banana יש 3 תנועות, והתוכנית מדפיסה 2. הריצו את הסימולציה בעמוד הפתרונות (או Debug ב-IntelliJ עם נקודת עצירה בתוך הלולאה) ובדקו אילו ערכים i מקבל.
banana has 3 vowels, and the program prints 2. Run the simulation on the solutions page (or Debug in IntelliJ with a breakpoint inside the loop) and check which values i takes.
סימולציה: הפתרון רץ שורה אחרי שורה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 {
static int vowels(String s) {
int n = 0;
for (int i = 0; i < s.length(); i++) {
if ("aeiou".indexOf(s.charAt(i)) >= 0) {
n++;
}
}
return n;
}
public static void main(String[] args) {
System.out.println(vowels("banana"));
}
}3
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מקסימום של שלילייםMax of negatives
תזכורת: מה ביקשוReminder: the task
max({-5, -2, -9}) מחזירה 0, שבכלל לא במערך. מצאו את הבאג ותקנו כך שיודפס -2.
max({-5, -2, -9}) returns 0, which isn't even in the array. Find the bug and fix it so it prints -2.
סימולציה: הפתרון רץ שורה אחרי שורה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 {
static int max(int[] a) {
int best = a[0];
for (int x : a) {
if (x > best) {
best = x;
}
}
return best;
}
public static void main(String[] args) {
System.out.println(max(new int[]{-5, -2, -9}));
}
}-2
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
the שלא נמצאThe the that isn't found
תזכורת: מה ביקשוReminder: the task
התוכנית סופרת כמה פעמים the מופיע, ומדפיסה 0 במקום 2. עצרו בתוך ה-if והסתכלו: w הוא "the", ובכל זאת התנאי false. למה?
The program counts how many times the appears, and prints 0 instead of 2. Stop inside the if and look: w is "the", yet the condition is false. Why?
סימולציה: הפתרון רץ שורה אחרי שורה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 text = "the cat and the hat";
int n = 0;
for (String w : text.split(" ")) {
if (w.equals("the")) {
n++;
}
}
System.out.println(n);
}
}2
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 11: שלושה באגיםShelf, step 11: three bugs
תזכורת: מה ביקשוReminder: the task
בקוד של המדף יש שלושה באגים. הראשון מפיל את התוכנית, ושני האחרים מסתתרים מאחוריו. תקנו אחד-אחד, והריצו אחרי כל תיקון. בסוף צריך להודפס Average: 4.333333333333333 ואז Found Emma.
The shelf code has three bugs. The first crashes the program, and the other two hide behind it. Fix them one at a time, running after each fix. In the end it should print Average: 4.333333333333333 then Found Emma.
סימולציה: הפתרון רץ שורה אחרי שורה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) {
String[] titles = {"Dune", "Emma", "Ulysses"};
int[] ratings = {5, 4, 4};
int sum = 0;
for (int i = 0; i < ratings.length; i++) {
sum += ratings[i];
}
System.out.println("Average: " + (double) sum / ratings.length);
String wanted = new String("Emma");
for (String t : titles) {
if (t.equals(wanted)) {
System.out.println("Found " + t);
}
}
}
}Average: 4.333333333333333 Found Emma
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
הבאג לא איפה שהוא נשברThe bug isn't where it breaks
תזכורת: מה ביקשוReminder: the task
התוכנית קורסת ב-parsePrice. אבל parsePrice בסדר: הבעיה היא בקלט, "2,00" עם פסיק. קראו את כל ה-stack trace (parsePrice ← total ← main), ותקנו את parsePrice כך שתקבל גם פסיק. צריך להודפס 12.5.
The program crashes in parsePrice. But parsePrice is fine: the problem is the input, "2,00" with a comma. Read the whole stack trace (parsePrice ← total ← main), and fix parsePrice to accept a comma too. It should print 12.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 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 {
static double parsePrice(String text) {
return Double.parseDouble(text.replace(",", "."));
}
static double total(String[] prices) {
double sum = 0;
for (String p : prices) {
sum += parsePrice(p);
}
return sum;
}
public static void main(String[] args) {
System.out.println(total(new String[]{"10.5", "2,00"}));
}
}12.5
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ראשוני מדיToo prime
תזכורת: מה ביקשוReminder: the task
התוכנית מדפיסה ראשוניים עד 10 ויוצא 1 2 3 4 5 7. יש שני באגים ב-isPrime. היעזרו בסימולציה (Step Into ל-isPrime(4)) ותקנו כך שיודפס 2 3 5 7.
The program prints primes up to 10 and gets 1 2 3 4 5 7. There are two bugs in isPrime. Use the simulation (Step Into isPrime(4)) and fix it so it prints 2 3 5 7.
סימולציה: הפתרון רץ שורה אחרי שורה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 {
static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
for (int n = 1; n <= 10; n++) {
if (isPrime(n)) {
System.out.print(n + " ");
}
}
System.out.println();
}
}2 3 5 7
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.