當前位置:編程學習大全網 - 編程軟體 - 用JAVA編程,輸入自己的生日,判斷自己生日是當年中的第幾天和星期幾。

用JAVA編程,輸入自己的生日,判斷自己生日是當年中的第幾天和星期幾。

import java.io.*;

import java.util.*;

public class GetBirth {

int year = 0;

int month = 0;

int day = 0;

Calendar cld = Calendar.getInstance();//創建壹個日歷

public GetBirth(){

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.println("請輸入年、月、日:");

try {

year = Integer.parseInt(in.readLine());

month = Integer.parseInt(in.readLine());

day = Integer.parseInt(in.readLine());

} catch (NumberFormatException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

//將日歷時間設置成生日那天

public void setDate(int year,int month,int day){

cld.set(year,month-1,day);

}

//獲取該日是壹年當中的第幾天

public int getDay(){

return cld.get(6);

}

//獲取該日是星期幾

public String getDate(){

int date = cld.get(7);

return getWeekday(date);

}

//判斷解析壹周7天的值

public String getWeekday(int dayofweek){

switch(dayofweek){

case 1: return "星期日";

case 2: return "星期壹";

case 3: return "星期二";

case 4: return "星期三";

case 5: return "星期四";

case 6: return "星期五";

case 7: return "星期六";

default:return "error";

}

}

public static void main(String[] args) {

GetBirth gb = new GetBirth();

gb.setDate(gb.year, gb.month, gb.day);

System.out.println(gb.getDay());

System.out.println(gb.getDate());

}

}

希望對樓主有所幫助,謝謝。

  • 上一篇:廣寧第壹中學的學校簡介
  • 下一篇:編程魔方代碼
  • copyright 2024編程學習大全網