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.
ברכה אישיתA personal greeting
תזכורת: מה ביקשוReminder: the task
קראו שם עם nextLine והדפיסו Hello, <name>!. (בבדיקה מוקלד Dana.)
Read a name with nextLine and print Hello, <name>!. (The check types Dana.)
סימולציה: הפתרון רץ שורה אחרי שורה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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name + "!");
}
}Name: Dana Hello, Dana!
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מחשבון חיבורAn adding calculator
תזכורת: מה ביקשוReminder: the task
קראו שני מספרים שלמים והדפיסו 7 + 5 = 12 (בבדיקה מוקלדים 7 ו-5).
Read two whole numbers and print 7 + 5 = 12 (the check types 7 and 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a + " + " + b + " = " + (a + b));
}
}7 5 7 + 5 = 12
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
קבוצת גילAge group
תזכורת: מה ביקשוReminder: the task
קראו גיל והדפיסו child (מתחת ל-13), teen (13–17) או adult (18 ומעלה).
Read an age and print child (under 13), teen (13–17) or adult (18 and over).
סימולציה: הפתרון רץ שורה אחרי שורה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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if (age < 13) {
System.out.println("child");
} else if (age < 18) {
System.out.println("teen");
} else {
System.out.println("adult");
}
}
}15 teen
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
צייד באגים: השם שנעלםBug hunter: the vanishing name
תזכורת: מה ביקשוReminder: the task
התוכנית קוראת גיל ואז שם, אבל מדפיסה 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).
סימולציה: הפתרון רץ שורה אחרי שורה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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
System.out.println(name + " is " + age);
}
}30 Dana Dana is 30
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
נחשו את המספרGuess the number
תזכורת: מה ביקשוReminder: the task
המספר הסודי הוא 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.
סימולציה: הפתרון רץ שורה אחרי שורה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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int secret = 7;
int guess = 0;
int tries = 0;
while (guess != secret) {
guess = sc.nextInt();
tries++;
if (guess < secret) {
System.out.println("Too low");
} else if (guess > secret) {
System.out.println("Too high");
}
}
System.out.println("Correct! " + tries + " tries");
}
}3 Too low 9 Too high 7 Correct! 3 tries
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 6: תפריטShelf, step 6: a menu
תזכורת: מה ביקשוReminder: the task
תפריט שקורא שורות: 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.)
סימולציה: הפתרון רץ שורה אחרי שורה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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringBuilder shelf = new StringBuilder();
int count = 0;
boolean running = true;
while (running) {
String cmd = sc.nextLine();
if (cmd.startsWith("add ")) {
if (count > 0) {
shelf.append(", ");
}
shelf.append(cmd.substring(4));
count++;
} else if (cmd.equals("list")) {
System.out.println("Books (" + count + "): " + shelf);
} else if (cmd.equals("quit")) {
running = false;
}
}
System.out.println("Bye");
}
}add Dune add Emma list Books (2): Dune, Emma quit Bye
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ממוצע עד אפסAverage until zero
תזכורת: מה ביקשוReminder: the task
קראו מספרים עד שמגיע 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.
סימולציה: הפתרון רץ שורה אחרי שורה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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int count = 0;
while (true) {
int n = sc.nextInt();
if (n == 0) {
break;
}
sum += n;
count++;
}
if (count == 0) {
System.out.println("No numbers");
} else {
System.out.println("Average: " + (double) sum / count);
}
}
}10 20 30 0 Average: 20.0
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
קלט עמידRobust input
תזכורת: מה ביקשוReminder: the task
בקשו מספר בין 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.)
סימולציה: הפתרון רץ שורה אחרי שורה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.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
while (choice < 1 || choice > 10) {
if (!sc.hasNextInt()) {
sc.next();
System.out.println("Not a number");
continue;
}
choice = sc.nextInt();
if (choice < 1 || choice > 10) {
System.out.println("Out of range");
}
}
System.out.println("You chose " + choice);
}
}abc Not a number 15 Out of range 4 You chose 4
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.