2014年12月26日 星期五

java程式設計題目---公式計算【判斷一年有幾天(使用method)】

Write a method that returns the number of days in a year using the following header:
public static int numberOfDaysInAYear(int year)
Write a test program that displays the number of days in year from 2000 to 2020.


  1. public class C1
  2. {
  3. public static void main(String[] args)
  4. {
  5. int year = 2000;
  6. for(int i = 1; i <= 21 ; i++ )//印出2000-2020共跑21次
  7. {
  8. System.out.printf("%-4d",year);//印出年分
  9. System.out.print("年 ");
  10. System.out.println(numberOfDaysInAYear(year) + "天");
  11. //呼叫 numberOfDaysInAYear 並回傳天數
  12. year = year + 1;
  13. }
  14. }
  15. public static int numberOfDaysInAYear(int year)
  16. {
  17. //判斷天數
  18. int a = year % 4;
  19. int b = year % 100;
  20. int c = year % 400;
  21. int day = 365 ;
  22. if(a == 0 || c == 0 && b != 0 )//為閏年
  23. {
  24. day = 366 ;
  25. }
  26. else//不為閏年
  27. {
  28. day = 365 ;
  29. }
  30. return day;//回傳結果
  31. }
  32. }

沒有留言:

張貼留言

Go(Golang)程式語言 設定GCC

下載MSYS2:  https://www.msys2.org/ 安裝 程式開始執行後輸入: pacman -Syu y y 在安裝目錄底下找到msys2.exe,雙擊執行 輸入指令: pacman -Su pacman -S --needed base-devel mingw-...