חלק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.
הנקודות החשובותKey points
- קלט: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה.input: What the user types into the program while it runs.
- Scanner: מחלקה שקוראת קלט:Scanner: A class that reads input:
- מייבאים את Scanner, יוצרים אחד על
System.in, וקוראים.Import Scanner, create one onSystem.in, and read. - התוכנית מחכה עד שמקלידים ולוחצים Enter.The program waits until you type and press Enter.
nextIntקורא רק את המספר ומשאיר את ה-Enter בצד.nextIntreads only the number and leaves the Enter behind.- ה-
nextLineשאחריו מקבל שורה ריקה!ThenextLineafter it gets an empty line! hasNextInt()שואל אם המילה הבאה היא מספר, בלי לקרוא אותה.hasNextInt()asks whether the next token is a number without reading it.- אם לא, קוראים אותה עם
next()וזורקים.If not, read it withnext()and drop it. - לולאה שקוראת פקודה,
switchשמחליט מה לעשות, ומשתנה שאומר מתי לצאת.A loop that reads a command, aswitchthat decides what to do, and a variable that says when to quit.
מונחיםTerms
- קלטinput: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה. בבדיקות כאן, הקלט מוקלד מראש.What the user types into the program while it runs. In the checks here, the input is typed in advance.
- ScannerScanner: מחלקה שקוראת קלט:
Scanner sc = new Scanner(System.in);, ואזsc.nextLine(),sc.nextInt()וכו'.A class that reads input:Scanner sc = new Scanner(System.in);, thensc.nextLine(),sc.nextInt()and so on. - מילה (token)token: חתיכת קלט בין רווחים או שורות.
nextIntו-nextקוראים מילה אחת, ו-nextLineקורא שורה שלמה.A piece of input between spaces or lines.nextIntandnextread one token;nextLinereads a whole line. - מלכודת nextIntthe nextInt trap:
nextIntקורא את המספר אבל משאיר את ה-Enter. ה-nextLineהבא קורא את השארית הריקה. הפתרון:nextLine()נוסף אחריnextInt.nextIntreads the number but leaves the Enter behind. The nextnextLinereads that empty leftover. The fix: an extranextLine()afternextInt. - בדיקת קלטinput validation: בודקים שהקלט הגיוני לפני שמשתמשים בו:
hasNextInt()לפניnextInt(), ובדיקת טווח.Checking input makes sense before using it:hasNextInt()beforenextInt(), and a range check. - importimport: שורה בראש הקובץ שמביאה מחלקה מספרייה:
import java.util.Scanner;.A line at the top of the file that brings in a library class:import java.util.Scanner;. - קלטinput: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה. בבדיקות כאן, הקלט מוקלד מראש.What the user types into the program while it runs. In the checks here, the input is typed in advance.
- ScannerScanner: מחלקה שקוראת קלט:
Scanner sc = new Scanner(System.in);, ואזsc.nextLine(),sc.nextInt()וכו'.A class that reads input:Scanner sc = new Scanner(System.in);, thensc.nextLine(),sc.nextInt()and so on. - מילה (token)token: חתיכת קלט בין רווחים או שורות.
nextIntו-nextקוראים מילה אחת, ו-nextLineקורא שורה שלמה.A piece of input between spaces or lines.nextIntandnextread one token;nextLinereads a whole line. - מלכודת nextIntthe nextInt trap:
nextIntקורא את המספר אבל משאיר את ה-Enter. ה-nextLineהבא קורא את השארית הריקה. הפתרון:nextLine()נוסף אחריnextInt.nextIntreads the number but leaves the Enter behind. The nextnextLinereads that empty leftover. The fix: an extranextLine()afternextInt. - בדיקת קלטinput validation: בודקים שהקלט הגיוני לפני שמשתמשים בו:
hasNextInt()לפניnextInt(), ובדיקת טווח.Checking input makes sense before using it:hasNextInt()beforenextInt(), and a range check. - importimport: שורה בראש הקובץ שמביאה מחלקה מספרייה:
import java.util.Scanner;.A line at the top of the file that brings in a library class:import java.util.Scanner;.
Cheatsheet
קריאת שורה ומספרReading a line and a number
Scanner sc = new Scanner(System.in);
System.out.print("Name: ");
String name = sc.nextLine();
System.out.print("Age: ");
int age = sc.nextInt();
System.out.println(name + " will be " + (age + 1) + " next year");המלכודת של nextIntThe nextInt trap
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
sc.nextLine();
String city = sc.nextLine();
System.out.println(age + " from " + city);בודקים לפני שקוראיםCheck before reading
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
String bad = sc.next();
System.out.println(bad + " is not a number");
}
int n = sc.nextInt();
System.out.println("Got " + n);תפריט שחוזרA menu that repeats
Scanner sc = new Scanner(System.in);
boolean running = true;
while (running) {
String cmd = sc.nextLine();
switch (cmd) {
case "hi" -> System.out.println("Hello!");
case "quit" -> running = false;
default -> System.out.println("Unknown: " + cmd);
}
}שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|