使用方法去完成 import scala.io.StdIn object work1 { def main(args: Array[String]): Unit = { // 1.先輸出提示語句,並接受用戶輸入的年,月 println("請輸入年:") var year = StdIn.readIn ...
使用方法去完成
import scala.io.StdIn object work1 { def main(args: Array[String]): Unit = { // 1.先輸出提示語句,並接受用戶輸入的年,月 println("請輸入年:") var year = StdIn.readInt() println("請輸入月:") var month = StdIn.readInt() // 2.根據用戶輸入的年,先判斷是否是閏年 def reiNian(nian: Int): Boolean = { if (nian % 400 == 0) { return true } if (nian % 4 == 0 && nian % 100 != 0) { return true } else false } // 3.根據用戶輸入的月來判斷月的天數 def getNowMonthDays(year: Int, month: Int): Int = { if (month == 4 || month == 6 || month == 9 || month == 11) { return 30 } else if (month == 4) { return if (reiNian(year)) 29 else 28; } else return 31 } // 4.用迴圈計算用戶輸入的年份距離1900年1月1日的總天數 def getFormNowYearTo1900TotalDays(year: Int): Int = { var sumdayyear = 0 for (i <- 1900 to year) { sumdayyear = sumdayyear + (if (reiNian(year)) 366 else 365) } return sumdayyear } // 5.用迴圈計算用戶輸入的月份距輸入的年份的1月1日共有多少天 def getNowYearToDays(year: Int, month: Int): Int = { var sumday = 0 for (i <- 1 to month) { sumday = sumday + getNowMonthDays(year, i) } return sumday } // 6.相加第4步與第5步的天數,得到總天數. var totalDaysY = getFormNowYearTo1900TotalDays(year); var totalDaysM = getNowYearToDays(year, month); var totalDays = totalDaysY + totalDaysM // 7.用(總天數+ 1)%7來計算輸入的月的第一的星期數 def getweek(totalDays: Int): Int = { return (totalDays + 1) % 7 } // 8.根據第7步得到的值格式化輸出這個月的日曆 def FormatCalendar(week: Int, year: Int, month: Int): Unit = { var cut = 0; //記數 判斷是否是到7 如果到7就換行 println("星期日\t\t星期一\t\t星期二\t\t星期三\t\t星期四\t\t星期五\t\t星期六") //星期數的列印 for (i <- 1 to week) { print("\t\t") cut = cut + 1 } //列印月份對應天數 for (i <- 1 to getNowMonthDays(year, month)) { //列印值 print(i + "\t\t"); cut = cut + 1 if (cut % 7 == 0) { println() } } } FormatCalendar(getweek(totalDays), year, month) } }
使用函數去完成
import scala.io.StdIn object work2 { def main(args: Array[String]): Unit = { // 1.先輸出提示語句,並接受用戶輸入的年,月 println("請輸入年:") var year = StdIn.readInt() println("請輸入月:") var month = StdIn.readInt() // 2.根據用戶輸入的年,先判斷是否是閏年 val reiNian=(year:Int)=> { if (year % 400 == 0) { true } if (year % 4 == 0 && year % 100 != 0) { true } else false } // 3.根據用戶輸入的月來判斷月的天數 val getNowMonthDays=(year: Int, month: Int) =>{ if (month == 4 || month == 6 || month == 9 || month == 11) { 30 } else if (month == 4) { if (reiNian(year)) 29 else 28 } else 31 } // 4.用迴圈計算用戶輸入的年份距離1900年1月1日的總天數 val getFormNowYearTo1900TotalDays=(year: Int)=>{ var sumdayyear:Int =0 for (i <- 1900 to year) { sumdayyear = sumdayyear + (if (reiNian(year)) 366 else 365) } println(sumdayyear) sumdayyear } println("****") // 5.用迴圈計算用戶輸入的月份距輸入的年份的1月1日共有多少天 val getNowYearToDays=(year:Int,month:Int)=> { var sumdaymonth:Int= 0 for (i <- 1 to month) { sumdaymonth = sumdaymonth + getNowMonthDays(year, i) } println("****") println(sumdaymonth) sumdaymonth } // 6.相加第4步與第5步的天數,得到總天數. var totalDaysY:Int = getFormNowYearTo1900TotalDays(year) var totalDaysM:Int = getNowYearToDays(year, month) var totalDays = totalDaysY + totalDaysM // 7.用(總天數+ 1)%7來計算輸入的月的第一的星期數 val getweek=(totalDays: Int)=> { (totalDays + 1) % 7 } // 8.根據第7步得到的值格式化輸出這個月的日曆 val FormatCalendar=(week: Int, year: Int, month: Int) => { var cut = 0; //記數 判斷是否是到7 如果到7就換行 println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六") //星期數的列印 for (i <- 1 to week) { print("\t\t") cut = cut + 1 } //列印月份對應天數 for (i <- 1 to getNowMonthDays(year, month)) { //列印值 print(i + "\t\t"); cut = cut + 1 if (cut % 7 == 0) { println() } } } FormatCalendar(getweek(totalDays), year, month) } }