都教授故鄉的曆法跟地球相當類似:
1.每5年會有一個閏年,每85年內有16個閏年,每255年內有 49個閏年
2.星球紀元第3年為第一個閏年。
3.一年有322 (or 323) 天,分為13個月,
第1,4,7,10月為大月,每月26天;
第2,5,8,11月為中月,每月25天;
第3,6,9,12月為小月,每月24天;
4.第13月為安息月,平年時,安息月有22天,閏年時,安息月有23天。
1.輸入一個星球紀元年分,判斷其為閏年或平年 (15%)
2.請列出該星球前100個閏年的年份。(15%)
3.輸入一個年份及月份,印出該月的天數。 (請使用 switch) (15%)
4.輸入年、月、日,判斷該日是否合法,合法則印出 true, 非法則印出 false (15%)
4題都在一個檔案內
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("臺灣師範大學機電工程系物件導向程式設計 Quiz3");
Console.WriteLine("1.輸入一個星球紀元年分,判斷其為閏年或平年。");
string input = Console.ReadLine();
int year_1 = int.Parse(input);
if ((year_1 + 2) % 255 == 0)
Console.WriteLine("星球紀元{0}年是閏年!", year_1);
else
{
if ((year_1 + 2) % 85 == 0)
Console.WriteLine("星球紀元{0}年是平年!", year_1);
else if ((year_1 + 2) % 5 == 0)
Console.WriteLine("星球紀元{0}年是閏年!", year_1);
else
Console.WriteLine("星球紀元{0}年是平年!", year_1);
}
Console.WriteLine("\n2.請列出該星球前100個閏年的年份。");
int year_count_2 = 1, leap_year_count_2 = 0;
while(true)
{
if (leap_year_count_2 == 100)
break;
else
{
if ((year_count_2 + 2) % 255 == 0)
{
Console.WriteLine("星球紀元{0}年!", year_count_2);
year_count_2++;
leap_year_count_2++;
}
else
{
if ((year_count_2 + 2) % 85 == 0)
year_count_2++;
else if ((year_count_2 + 2) % 5 == 0)
{
Console.WriteLine("星球紀元{0}年!", year_count_2);
year_count_2++;
leap_year_count_2++;
}
else
year_count_2++;
}
}
}
Console.WriteLine("\n3.輸入一個年份及月份,印出該月的天數。");
input = Console.ReadLine();
int year_3 = int.Parse(input);
input = Console.ReadLine();
int month_3 = int.Parse(input);
switch (month_3)
{
case 1:
case 4:
case 7:
case 10:
Console.WriteLine("星球紀元{0}年{1}月有26天!", year_3, month_3);
break;
case 2:
case 5:
case 8:
case 11:
Console.WriteLine("星球紀元{0}年{1}月有25天!", year_3, month_3);
break;
case 3:
case 6:
case 9:
case 12:
Console.WriteLine("星球紀元{0}年{1}月有24天!", year_3, month_3);
break;
case 13:
if ((year_3 + 2) % 255 == 0)
Console.WriteLine("星球紀元{0}年{1}月有23天!", year_3, month_3);
else
{
if ((year_3 + 2) % 85 == 0)
Console.WriteLine("星球紀元{0}年{1}月有22天!", year_3, month_3);
else if ((year_3 + 2) % 5 == 0)
Console.WriteLine("星球紀元{0}年{1}月有23天!", year_3, month_3);
else
Console.WriteLine("星球紀元{0}年{1}月有22天!", year_3, month_3);
}
break;
default:
Console.WriteLine("輸入錯誤 沒有這個月份!");
break;
}
Console.WriteLine("\n4.輸入年、月、日,判斷該日是否合法,合法則印出 true, 非法則印出 false。");
input = Console.ReadLine();
int year_4 = int.Parse(input);
input = Console.ReadLine();
int month_4 = int.Parse(input);
input = Console.ReadLine();
int day_4 = int.Parse(input);
switch (month_4)
{
case 1:
case 4:
case 7:
case 10:
if (day_4 > 26 || day_4 < 1)
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是非法的 false!", year_4, month_4, day_4);
break;
}
else
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是合法的 true!", year_4, month_4, day_4);
break;
}
case 2:
case 5:
case 8:
case 11:
if (day_4 > 25 || day_4 < 1)
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是非法的 false!", year_4, month_4, day_4);
break;
}
else
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是合法的 true!", year_4, month_4, day_4);
break;
}
case 3:
case 6:
case 9:
case 12:
if (day_4 > 24 || day_4 < 1)
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是非法的 false!", year_4, month_4, day_4);
break;
}
else
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是合法的 true!", year_4, month_4, day_4);
break;
}
case 13:
if (day_4 > 23 || day_4 < 1)
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是非法的 false!", year_4, month_4, day_4);
break;
}
else if (day_4 == 23)
{
if ((year_4 + 2) % 255 == 0)
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是合法的 true!", year_4, month_4, day_4);
break;
}
else
{
if ((year_4 + 2) % 85 == 0)
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是非法的 false!", year_4, month_4, day_4);
break;
}
else if ((year_4 + 2) % 5 == 0)
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是合法的 true!", year_4, month_4, day_4);
break;
}
else
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是非法的 false!", year_4, month_4, day_4);
break;
}
}
}
else
{
Console.WriteLine("星球紀元{0}年{1}月{2}日是合法的 true!", year_4, month_4, day_4);
break;
}
default:
Console.WriteLine("星球紀元{0}年{1}月{2}日是非法的 false!", year_4, month_4, day_4);
break;
}
Console.ReadKey();
}
}
}
沒有留言:
張貼留言