اذهب إلى المحتوى
  • 0

برنامج يستقبل عمر المستخدم بالسنوات ويحسب عمره بالشهور والايام والساعات جافا

عائشه الجهلي

السؤال

Recommended Posts

  • 0

يُمكنك إستخدام الأفكار التالية لإنشاء البرنامج:

لطباعة جملة نستخدم التعليمة System.out.println فمثلاً لسؤال المستخدم عن إسمه يُمكننا إستخدام:

System.out.println("Enter your name: ");

لإستقبال مُدخلات من المُستخدم نستعمل الحزمة Scanner نقوم بإستيرادها:

import java.util.Scanner;

ثم نُنشئ كائن من الصنف Scanner:

Scanner scanner = new Scanner(System.in);

الآن إذا أردنا إستقبال و تخزين إسم المستخدم نقوم بالتالي بعد سؤاله:

Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name: ");
String name = scanner.nextLine();

و هنا عند تشغيل البرنامج سيتم طباعة الجملة و الإنتظار لحين إدخال أي شيء من طرف المُستخدم و الضغط على enter تُخزن في المُتغير name.

بعد ذلك يُمكننا سؤال المُستخدم عن عمره و إستقبال المُدخلات في مُتغير:

System.out.println("Enter your age: ");
int ageInYears = scanner.nextInt();

الآن بما أنه لديك كافة البيانات لحساب العُمر بأي وحدة فمثلاً العُمر بالاشهر هو حاصل ضرب العمر بالسنوات في 12:

int ageInMonths = ageInYears * 12;

و العمر بالأيام هو حاصل ضرب العمر بالسنوات في 365 لأن السنة بها 365 يوم:

int ageInDays = ageInYears * 365;

و العمر بالساعات هو حاصل ضرب العمر بالأيام في 24 بعد الحساب يُمكنك الطباعة بأي شكل تريدين:

System.out.println("Hello " + name +
                   "\nYour Age in:\nYears: " + ageInYears +
                   "\nMonths: " + ageInMonths +
                   "\nDays: " + ageInDays +
                   "\nHours: " + ageInHours
                  );
رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

هيك البرنامج كامل 

import java.util.Scanner;


public class JavaApplication16 {
    
    static void Age(int current_date, int current_month,
                    int current_year, int birth_date,
                    int birth_month, int birth_year)
    {
        int month[] = { 31, 28, 31, 30, 31, 30, 31,
                             31, 30, 31, 30, 31 };
 

        if (birth_date > current_date) {
            current_month = current_month - 1;
            current_date = current_date + month[birth_month - 1];
        }
        if (birth_month > current_month) {
            current_year = current_year - 1;
            current_month = current_month + 12;
        }
 
        int calculated_date = current_date - birth_date;
        int calculated_month = current_month - birth_month;
        int calculated_year = current_year - birth_year;
 

        System.out.println("Present Age");
        System.out.println("Years: " + calculated_year +
              " Months: " + calculated_month + " Days: " +
              calculated_date);
    }

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);  
        
        // إدخال التاريخ الحالي
        
        int current_date = sc.nextInt();
        int current_month = sc.nextInt();
        int current_year = sc.nextInt();
 
        //إدخال تاريخ الميلاد
        int birth_date = sc.nextInt();;
        int birth_month = sc.nextInt();;
        int birth_year = sc.nextInt();;
 
        // استدعاء التابع الذي قمنا بتعريفه لكي يطبع لنا العمر
        Age(current_date, current_month, current_year,
              birth_date, birth_month, birth_year);
        
        
    }
 
}

 اذا حابة شرح  خبريني  بتوفيق

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

جزاك الله خير وم قصرت تسلم كتييييير وف ميزان الحسنات اول م يكون عندي استشارة أن شاء الله برجع ليك... لانو ي داب مبتدئه ف الحاسوب 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

معليش بس ممكن تشرح لي البرنامج الكتبتو 

 

بتاريخ 7 ساعات قال Suhaib Hassan:

هيك البرنامج كامل 


import java.util.Scanner;


public class JavaApplication16 {
    
    static void Age(int current_date, int current_month,
                    int current_year, int birth_date,
                    int birth_month, int birth_year)
    {
        int month[] = { 31, 28, 31, 30, 31, 30, 31,
                             31, 30, 31, 30, 31 };
 

        if (birth_date > current_date) {
            current_month = current_month - 1;
            current_date = current_date + month[birth_month - 1];
        }
        if (birth_month > current_month) {
            current_year = current_year - 1;
            current_month = current_month + 12;
        }
 
        int calculated_date = current_date - birth_date;
        int calculated_month = current_month - birth_month;
        int calculated_year = current_year - birth_year;
 

        System.out.println("Present Age");
        System.out.println("Years: " + calculated_year +
              " Months: " + calculated_month + " Days: " +
              calculated_date);
    }

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);  
        
        // إدخال التاريخ الحالي
        
        int current_date = sc.nextInt();
        int current_month = sc.nextInt();
        int current_year = sc.nextInt();
 
        //إدخال تاريخ الميلاد
        int birth_date = sc.nextInt();;
        int birth_month = sc.nextInt();;
        int birth_year = sc.nextInt();;
 
        // استدعاء التابع الذي قمنا بتعريفه لكي يطبع لنا العمر
        Age(current_date, current_month, current_year,
              birth_date, birth_month, birth_year);
        
        
    }
 
}

 اذا حابة شرح  خبريني  بتوفيق

ممكن تشرح لي الكتبتو عشان ما مطابق للبرنامج حقي 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 18 ساعات قال عائشه الجهلي:

معليش بس ممكن تشرح لي البرنامج الكتبتو 

 

ممكن تشرح لي الكتبتو عشان ما مطابق للبرنامج حقي 

 

في البداية نحن داخل كلاس اسمة JavaApplication16 

يوجد فية ميثود اسمها Age يتاخد اكتر من مدخل   (  اليوم الاحالي و الشهر الحالي و السنة الحالية   و  يوم الميلاد شهر الميلاد وسنة الميلاد)

int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

هذه المصفوفة هي  عدد الاشهر وكل شهر كم يوم فية

if (birth_date > current_date) {
current_month = current_month - 1;
current_date = current_date + month[birth_month - 1];
}

هذا الشرط بيفحص اذا  كان  يوم الميلاد أكبر من اليوم الحالي (يوم في المستقبل )   يخلي  رقم الشهر - واحد 

وايضا    يعطي قية  current data بيعطي نفس القيمة الدخلة 

 

if (birth_month > current_month) { 
current_year = current_year - 1; 
current_month = current_month + 12; 
}

اما في هذا الشرط فيقوم بفحص الاشهر الدخلة  فيقوم بفحص الاشهر 

اذا كان الشهر المخل أقل من شهر الميلاد مثال شهر ميلادي 7 ونحن في شهر 5  اذا  لم أبغل عام كامل بعد لهذا يقوم بطرح  واحد من السنة   

ونفس الفكرة في الشرط في الاعلى 

int calculated_date = current_date - birth_date;
int calculated_month = current_month - birth_month;
int calculated_year = current_year - birth_year;

هنا يقوم بطرح القيم مب بعض يحصل على الناج النهائي  للعمر 

 

public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
// إدخال التاريخ الحالي 
int current_date = sc.nextInt(); 
int current_month = sc.nextInt();
int current_year = sc.nextInt(); 
//إدخال تاريخ الميلاد 
int birth_date = sc.nextInt();
int birth_month = sc.nextInt();
int birth_year = sc.nextInt();

 الان نحن داخل المكان الذي سيتم تنفيذ فية  

نطلب من المستخدم إدخل تريخ اليوم وتاريخ ميلاده 

Age(current_date, current_month, current_year, birth_date, birth_month, birth_year)

هنا نقوم بستدعاء الدالة التي كنت اشرح عنها قبل قليل في الاعلى 

وهذا كل شي  اتمنى  ان تكون الامو واضحه 

حياتي 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ منذ ساعة مضت قال Suhaib Hassan:

 

في البداية نحن داخل كلاس اسمة JavaApplication16 

يوجد فية ميثود اسمها Age يتاخد اكتر من مدخل   (  اليوم الاحالي و الشهر الحالي و السنة الحالية   و  يوم الميلاد شهر الميلاد وسنة الميلاد)


int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

هذه المصفوفة هي  عدد الاشهر وكل شهر كم يوم فية


if (birth_date > current_date) {
current_month = current_month - 1;
current_date = current_date + month[birth_month - 1];
}

هذا الشرط بيفحص اذا  كان  يوم الميلاد أكبر من اليوم الحالي (يوم في المستقبل )   يخلي  رقم الشهر - واحد 

وايضا    يعطي قية  current data بيعطي نفس القيمة الدخلة 

 


if (birth_month > current_month) { 
current_year = current_year - 1; 
current_month = current_month + 12; 
}

اما في هذا الشرط فيقوم بفحص الاشهر الدخلة  فيقوم بفحص الاشهر 

اذا كان الشهر المخل أقل من شهر الميلاد مثال شهر ميلادي 7 ونحن في شهر 5  اذا  لم أبغل عام كامل بعد لهذا يقوم بطرح  واحد من السنة   

ونفس الفكرة في الشرط في الاعلى 


int calculated_date = current_date - birth_date;
int calculated_month = current_month - birth_month;
int calculated_year = current_year - birth_year;

هنا يقوم بطرح القيم مب بعض يحصل على الناج النهائي  للعمر 

 


public static void main(String[] args) { 
Scanner sc=new Scanner(System.in); 
// إدخال التاريخ الحالي 
int current_date = sc.nextInt(); 
int current_month = sc.nextInt();
int current_year = sc.nextInt(); 
//إدخال تاريخ الميلاد 
int birth_date = sc.nextInt();
int birth_month = sc.nextInt();
int birth_year = sc.nextInt();

 الان نحن داخل المكان الذي سيتم تنفيذ فية  

نطلب من المستخدم إدخل تريخ اليوم وتاريخ ميلاده 


Age(current_date, current_month, current_year, birth_date, birth_month, birth_year)

هنا نقوم بستدعاء الدالة التي كنت اشرح عنها قبل قليل في الاعلى 

وهذا كل شي  اتمنى  ان تكون الامو واضحه 

حياتي 

جزيت خيرا 

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...