Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي الثالث - حل التمرين الثاني بلغة جافا

المطلوب

أكتب برنامج يطلب من المستخدم إدخال رقم الشهر, و بعدها سيقوم بطباعة إسم الشهر باللغة الإنجليزية.
رقم الشهر يجب أن يكون بين 1 و 12.
في حال قام المستخدم بإدخال رقم أصغر من 1 أو أكبر من 12 سيتم عرض الرسالة التالية له "Error input, Month number should be between 1 and 12.".

مثال: إذا قام المستخدم بإدخال الرقم 5 فستكون النتيجة كالتالي.

Month number: 5
Month name: May
	


الحل بلغة جافا

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int month;
System.out.print("Month number: ");
month = input.nextInt();
System.out.print("Month name: ");
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Error input, Month number should be between 1 and 12.");
break;
}
}
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int month; System.out.print("Month number: "); month = input.nextInt(); System.out.print("Month name: "); switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Error input, Month number should be between 1 and 12."); break; } } }

سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 5 عند التشغيل.

Month number: 5
Month name: May
		

سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 13 عند التشغيل.

Month number: 13
Month name: Error input, Month number should be between 1 and 12.