2014年7月12日 星期六

java程式設計題目---日期處理【印製月曆(一月)】

請設計一個程式可以印製某一年一月的月曆,請使用者輸入年份及當年第一天(1月1日)是星期幾,其中0代表星期日,1至6分別代表星期一至星期六。範例如下:














  1. import java.util.Scanner;
  2.  
  3. public class C1
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. Scanner input = new Scanner(System.in);
  9. //提示使用者輸入年份和當年第一天資訊
  10. System.out.println("使用者輸入:");
  11. String year = input.next();
  12. int a = input.nextInt();
  13. int i;
  14. int count = a + 1;
  15. //印出月曆抬頭
  16. System.out.println(year+"年1月份");
  17. System.out.println("Sun" + " " + "Mon" + " " + "Tue" + " " + "Wed" +
  18. " " + "Thu" + " " + "Fri" + " " +"Sat");
  19.  
  20. //設定該年一月月曆是從星期幾開始
  21. if( a == 1 )
  22. {
  23. System.out.print(" ");
  24. }
  25. if( a == 2)
  26. {
  27. System.out.print(" " + " " );
  28. }
  29. if( a == 3)
  30. {
  31. System.out.print(" " + " " + " " );
  32. }
  33. if( a == 4)
  34. {
  35. System.out.print(" " + " " + " " + " ");
  36. }
  37. if( a == 5)
  38. {
  39. System.out.print(" " + " " + " " + " " + " ");
  40. }
  41. if( a == 6)
  42. {
  43. System.out.print(" " + " " + " " + " " + " " + " ");
  44. }
  45. //輸出結果
  46. for( i=1 ; i <= 31 ; i++ )
  47. {
  48. if(count % 7 == 1 )
  49. {
  50. System.out.printf("\n%3d",i); //在這裡會換行
  51. }
  52. else if(count % 7 == 0)
  53. {
  54. System.out.printf("%5d",i);
  55. }
  56. else if(count % 7 == 2)
  57. {
  58. System.out.printf("%5d",i);
  59. }
  60. else if(count % 7 == 3)
  61. {
  62. System.out.printf("%5d",i);
  63. }
  64. else if(count % 7 == 4)
  65. {
  66. System.out.printf("%5d",i);
  67. }
  68. else if(count % 7 == 5)
  69. {
  70. System.out.printf("%5d",i);
  71. }
  72. else if(count % 7 == 6)
  73. {
  74. System.out.printf("%5d",i);
  75. }
  76. count = count + 1 ;
  77. }
  78. }
  79.  
  80. }

沒有留言:

張貼留言

Go(Golang)程式語言 設定GCC

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