انتقل إلى المحتوى
أمين أبوزيد
BEG-011

الإدخال والإخراج

  • 6 درس
  • 71 دقيقة قراءة
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • Scanner
  • الإدخال من الطرفية
  • التنسيق
  • التحليل (parsing)
  • التحقق من صحة المدخلات

تعلّم أولًا

71 دقيقة قراءة · 3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Read text and numbers from the console with Scanner.
  2. Parse strings to numbers and handle input that is not a number (looping until valid, without exceptions yet).
  3. Format output with printf/String.format (width, decimals, alignment).
  4. Explain why user input must never be trusted.

الدروس

  1. 01قراءة المدخلات باستخدام Scanner13 د قراءة
  2. 02فخ nextInt / nextLine11 د قراءة
  3. 03تحويل النصوص إلى أعداد12 د قراءة
  4. 04تنسيق المخرجات11 د قراءة
  5. 05التحقق من المدخلات: لا تثق بالمستخدم أبداً12 د قراءة
  6. 06عرض عملي: بناء طابعة الإيصالات مع التحقق من المدخلات12 د قراءة

المختبر والتمارين

Lab

BEG-011-LAB1 · Receipt printer

Outcomes: 1–4 · Time: ~3 h Read item names, quantities, and prices. Print an aligned receipt with line totals, subtotal, and tax. Reject invalid quantities (not a whole number, or less than 1) by asking again.

Example output:

text
Item        Qty    Price     Total
Pen           2     1.50      3.00
Notebook      1     4.25      4.25
Subtotal                      7.25
Tax (15%)                     1.09
Total                         8.34

Acceptance: columns aligned; invalid input re-prompted.

Exercises (assessment: five)

IDOutcomeTask
BEG-011-EX011Ask for a name and an age; print Ana is 30 years old.
BEG-011-EX021Ask for age and then full name, and make both work correctly (the trap).
BEG-011-EX032Read a decimal number as text and double it with Double.parseDouble.
BEG-011-EX042Keep asking for a whole number until the user enters one between 1 and 10.
BEG-011-EX053Print three products and prices in aligned columns with two decimals.
أظهر الحلول
  • EX01 in.nextLine() for name, in.nextInt() for age, then printf("%s is %d years old.%n", name, age).
  • EX02 Add in.nextLine(); after nextInt() before reading the name.
  • EX03 double d = Double.parseDouble(in.nextLine()); System.out.println(d * 2);
  • EX04 Loop with hasNextInt(), discard bad input with in.next(), and check n >= 1 && n <= 10.
  • EX05 printf("%-12s %8.2f%n", name, price).

Outcome 4 is checked by the quiz (Q09, Q10) and by lab criterion "invalid input re-prompted".

الاختبار

Outcomes: O1 Scanner · O2 parse and validate · O3 formatting · O4 never trust input.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Which reads a whole line?O1
  2. 2To use Scanner you must:O1
  3. 3nextLine() returns empty right after nextInt() because:O1
  4. 4Integer.parseInt("12") + 1 is:O2
  5. 5in.hasNextInt() tells you:O2
  6. 6%.2f prints:O3
  7. 7%-10s does what?O3
  8. 8%n in printf is:O3
  9. 9Why validate input?O4
  10. 10Best response to the quantity abc:O4

الجزء العملي

Five exercises BEG-011-EX01 to EX05 plus the receipt printer. Pass: at least four exercises and a lab that re-prompts on invalid input.

أخطاء شائعة

Skipped input after nextInt; crashing on non-numeric input; misaligned columns.

التقييم

5 exercises.

الصلة بالمسار الوظيفي

Input validation is the seed of JNR-014 (Bean Validation) and JNR-041 (injection).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.