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

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

Scanner וקלטScanner & input

קריאת קלט עם Scanner, nextLine מול nextInt והמלכודת שביניהם, בדיקת קלט ותפריט טקסט ראשון.

Reading input with Scanner, nextLine vs nextInt and the trap between them, validating input and a first text menu.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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. מה צריך בראש הקובץ כדי להשתמש ב-Scanner?What does the top of the file need to use Scanner?

2. הקלט הוא 30 ואז Haifa. הקוד: int a = sc.nextInt(); String c = sc.nextLine();. מה ב-c?The input is 30 then Haifa. The code: int a = sc.nextInt(); String c = sc.nextLine();. What's in c?

3. מה יקרה עם sc.nextInt() כשהמשתמש מקליד abc?What happens with sc.nextInt() when the user types abc?

תרגיליםExercises

תרגילExercise 1

ברכה אישיתA personal greeting

קראו שם עם nextLine והדפיסו Hello, <name>!. (בבדיקה מוקלד Dana.)

Read a name with nextLine and print Hello, <name>!. (The check types Dana.)

איך הפלט אמור להיראות?What should the output look like?
Name: Dana
Hello, Dana!
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: Scanner sc = new Scanner(System.in); ו-import java.util.Scanner; בראש הקובץ.Scanner sc = new Scanner(System.in); plus import java.util.Scanner; at the top.
רמזHint 2: String name = sc.nextLine();String name = sc.nextLine();
לפתרוןSolution
תרגילExercise 2

מחשבון חיבורAn adding calculator

קראו שני מספרים שלמים והדפיסו 7 + 5 = 12 (בבדיקה מוקלדים 7 ו-5).

Read two whole numbers and print 7 + 5 = 12 (the check types 7 and 5).

איך הפלט אמור להיראות?What should the output look like?
7
5
7 + 5 = 12
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 a = sc.nextInt(); פעמיים.int a = sc.nextInt(); twice.
לפתרוןSolution
תרגילExercise 3

קבוצת גילAge group

קראו גיל והדפיסו child (מתחת ל-13), teen (13–17) או adult (18 ומעלה).

Read an age and print child (under 13), teen (13–17) or adult (18 and over).

איך הפלט אמור להיראות?What should the output look like?
15
teen
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 (age < 13) ... else if (age < 18) ... else ...if (age < 13) ... else if (age < 18) ... else ...
לפתרוןSolution
תרגילExercise 4

צייד באגים: השם שנעלםBug hunter: the vanishing name

התוכנית קוראת גיל ואז שם, אבל מדפיסה is 30 בלי השם. תקנו (בבדיקה מוקלדים 30 ו-Dana).

The program reads an age then a name, but prints is 30 with no name. Fix it (the check types 30 and Dana).

איך הפלט אמור להיראות?What should the output look like?
30
Dana
Dana is 30
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: מה נשאר בקלט אחרי nextInt?What's left in the input after nextInt?
רמזHint 2: מוסיפים sc.nextLine(); מיד אחרי nextInt.Add sc.nextLine(); right after nextInt.
לפתרוןSolution
תרגילExercise 5

נחשו את המספרGuess the number

המספר הסודי הוא 7. קראו ניחושים בלולאה עד שמנחשים נכון: על כל ניחוש הדפיסו Too low או Too high, ובסוף Correct! 3 tries (עם מספר הניסיונות). בבדיקה מוקלדים 3, 9, 7.

The secret is 7. Read guesses in a loop until right: for each guess print Too low or Too high, and finally Correct! 3 tries (with the number of tries). The check types 3, 9, 7.

איך הפלט אמור להיראות?What should the output look like?
3
Too low
9
Too high
7
Correct! 3 tries
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 guess = 0; int tries = 0; while (guess != secret) { ... }int guess = 0; int tries = 0; while (guess != secret) { ... }
רמזHint 2: בתוך הלולאה: קוראים, מקדמים את tries, ומשווים.Inside the loop: read, bump tries, and compare.
לפתרוןSolution
תרגילExercise 6

מדף, צעד 6: תפריטShelf, step 6: a menu

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

תפריט שקורא שורות: add <title> מוסיף ספר, list מדפיס Books (2): Dune, Emma, quit יוצא ומדפיס Bye. שמרו את הרשימה ב-StringBuilder ומונה. (בבדיקה: add Dune, add Emma, list, quit.)

A menu reading lines: add <title> adds a book, list prints Books (2): Dune, Emma, quit leaves and prints Bye. Keep the list in a StringBuilder and a counter. (The check: add Dune, add Emma, list, quit.)

איך הפלט אמור להיראות?What should the output look like?
add Dune
add Emma
list
Books (2): Dune, Emma
quit
Bye
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 (cmd.startsWith("add ")) ואז cmd.substring(4) הוא שם הספר.if (cmd.startsWith("add ")) then cmd.substring(4) is the title.
רמזHint 2: לפני כל ספר חוץ מהראשון מוסיפים ", ".Before every book except the first, append ", ".
לפתרוןSolution

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

אתגרChallenge 1

ממוצע עד אפסAverage until zero

קראו מספרים עד שמגיע 0, והדפיסו את הממוצע שלהם (בלי ה-0): Average: 20.0. אם האפס הוא המספר הראשון, הדפיסו No numbers.

Read numbers until a 0 arrives, and print their average (without the 0): Average: 20.0. If the 0 comes first, print No numbers.

איך הפלט אמור להיראות?What should the output look like?
10
20
30
0
Average: 20.0
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: לולאה while (true) עם break כשקוראים 0.A while (true) loop with break on 0.
רמזHint 2: סכום ומונה, ובסוף (double) sum / count.A sum and a counter, and at the end (double) sum / count.
לפתרוןSolution
אתגרChallenge 2

קלט עמידRobust input

בקשו מספר בין 1 ל-10. על משהו שאינו מספר הדפיסו Not a number, על מספר מחוץ לטווח Out of range, וחזרו לבקש. על מספר טוב: You chose 4. (בבדיקה: abc, 15, 4.)

Ask for a number from 1 to 10. For something that isn't a number print Not a number, for a number out of range Out of range, and ask again. For a good number: You chose 4. (The check: abc, 15, 4.)

איך הפלט אמור להיראות?What should the output look like?
abc
Not a number
15
Out of range
4
You chose 4
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 (!sc.hasNextInt()) { sc.next(); ... continue; }if (!sc.hasNextInt()) { sc.next(); ... continue; }
רמזHint 2: אחרי nextInt בודקים טווח.After nextInt, check the range.
לפתרוןSolution

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