Java
 Computer >> コンピューター >  >> プログラミング >> Java

誕生日を確認してお誕生日おめでとうメッセージを印刷するJavaプログラム


この記事では、誕生日を確認してお誕生日おめでとうメッセージを印刷する方法を理解します。誕生日の確認は、現在の日と指定された誕生日を比較することで実行されます。

以下は同じのデモンストレーションです-

入力

入力が-

であると仮定します
Birthday Date: 15 July

出力

必要な出力は-

になります
Today’s Date is 20-12-2021
Today is not my birthday

アルゴリズム

Step 1 - START
Step 2 - Declare variables for month and dates values namely month_of_birth and
date_of_birth
Step 3 - Read the required values from the user/ define the values
Step 4 - Use LocalDate.now() function to get the current date and store it in the variable.
Step 5 – Using an if loop, compare the current month and date value with the input date and
month values respectively. If the values match, the result is true.
Step 5- Display the result
Step 6- Stop

例1

import java.time.LocalDate;
import java.time.Month;
public class HappyBirthday {
   public static void main(String args[]) {
      int date_of_birth = 15;
      Month month_of_birth = Month.JULY;
      System.out.println("The required packages have been imported");
      LocalDate current_date = LocalDate.now();
      System.out.println("Today's Date is " + current_date);
      System.out.println("The birthday is defined as : " +date_of_birth + " " +month_of_birth);
      int date = current_date.getDayOfMonth();
      Month month = current_date.getMonth();
      if(date == date_of_birth && month == month_of_birth) {
         System.out.println("HAPPY BIRTHDAY TO YOU !!");
      } else {
         System.out.println("Your birthday is not today ");
      }
   }
}

出力

The required packages have been imported
Today's Date is 2022-02-09
The birthday is defined as : 15 JULY
Your birthday is not today

例2

ここでは、整数は事前に定義されており、その値にアクセスしてコンソールに表示されます。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Birthday {
   public static void main(String[] args) throws ParseException {
      SimpleDateFormat s = new SimpleDateFormat("MM-dd");
      Date today = s.parse("10-15");
      Date my_birthday_date = s.parse("10-15");
      System.out.println("The birthday date is defined as October 15th" );
      if (today.compareTo(my_birthday_date) == 0) {
         System.out.println("Happy Birthday!!");
      } else {
         System.out.println("Today is not tour birthday");
      }
   }
}

出力

The birthday date is defined as October 15th
Happy Birthday!!

  1. 直方体の表面積と体積を見つけるJavaプログラム

    この記事では、直方体の表面積と体積を計算する方法を理解します。直方体は、長方形の6つの面を持つ3次元オブジェクトです。つまり、辺の長さと幅が異なります。立方体と直方体の違いは、立方体の長さ、高さ、幅が等しいのに対し、直方体ではこれら3つは同じではないことです 直方体の表面積は、式-を使用して計算されます。 2*( length *width + width* height + height*length) 直方体の面積は、式-を使用して計算されます。 length*width*height 以下は同じのデモンストレーションです- 入力 入力が-であると仮定します Length

  2. 日付が有効かどうかを確認し、有効な場合は増分日を出力するPythonプログラム

    日付が有効かどうかを確認し、有効な日付の場合は増分日を出力する必要がある場合は、「if」条件が使用されます。 以下は同じのデモンストレーションです- 例 my_date = input("Enter a date : ") dd,mm,yy = my_date.split('/') dd=int(dd) mm=int(mm) yy=int(yy) if(mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12):    max_val = 31 elif(mm==4 or