Homework1_1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1_1
{
class Homework1_1
{
static void Main(string[] args)
{
int temp;
int sum;
for (int i = 100; i < 1000; i++)
{
sum = 0;
temp = i;
while (temp != 0)
{
sum += (temp % 10) * (temp % 10) * (temp % 10);
temp = temp / 10;
}
if (sum == i)
Console.WriteLine("{0}",i);
}
Console.ReadLine();
}
}
}
Homework1_2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1_2
{
class Homework1_2
{
static void Main(string[] args)
{
while(true)
{
Console.WriteLine("欲離開請輸入0");
Console.WriteLine("請輸入年份");
int year = int.Parse(Console.ReadLine());
if (year == 0)
{
break;
}
else if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
Console.WriteLine("閏年");
}
else
{
Console.WriteLine("平年");
}
Console.ReadKey();
Console.Clear();
}
}
}
}
Homework1_3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1_3
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1_3
{
class Homework1_3
{
static void Main(string[] args)
{
Console.WriteLine("欲離開請輸入0");
while (true)
{
Console.WriteLine("請輸入年");
int year = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入月");
int month = int.Parse(Console.ReadLine());
if (year <= 0 || month <= 0 || month > 12)
{
break;
}
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
Console.WriteLine("31天");
break;
case 4:
case 6:
case 9:
case 11:
Console.WriteLine("30天");
break;
case 2:
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
Console.WriteLine("29天");
}
else
{
Console.WriteLine("28天");
}
break;
default:
break;
}
Console.ReadKey();
Console.Clear();
}
}
}
}
Homework1_4
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1_4
{
class Homework1_4
{
static void Main(string[] args)
{
Console.WriteLine("欲離開請輸入0");
while (true)
{
Console.WriteLine("請輸入年");
int year = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入月");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入日");
int date = int.Parse(Console.ReadLine());
if (year <= 0 || month <= 0 || date <= 0 || month > 12)
{
break;
}
if (month == 1 || month == 2) //利用基姆拉爾森公式,1.2月當作去年的13.14月
{
year -= 1;
month += 12;
}
int day = (year + year / 4 + year / 400 - year / 100 + 2*month + 3*(month+1)/5 + date + 1) % 7;
switch (day)
{
case 0:
Console.WriteLine("星期日");
break;
case 1:
Console.WriteLine("星期一");
break;
case 2:
Console.WriteLine("星期二");
break;
case 3:
Console.WriteLine("星期三");
break;
case 4:
Console.WriteLine("星期四");
break;
case 5:
Console.WriteLine("星期五");
break;
case 6:
Console.WriteLine("星期六");
break;
}
Console.ReadKey();
Console.Clear();
}
}
}
}
Homework1_5
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1_5
{
class Homework1_5
{
static void Main(string[] args)
{
Console.WriteLine("欲離開請輸入0");
while (true)
{
Console.WriteLine("請輸入年");
int year = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入月");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入日");
int date = int.Parse(Console.ReadLine());
if (year <= 0 || month <= 0 || date <= 0 || month > 12)
{
Console.WriteLine("false");
break;
}
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (date > 0 && date <= 31)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
break;
case 4:
case 6:
case 9:
case 11:
if (date > 0 && date <= 30)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
break;
case 2:
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
if (date > 0 && date <= 29)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}
else
{
if (date > 0 && date <= 28)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}
break;
default:
break;
}
Console.ReadKey();
Console.Clear();
}
}
}
}
Homework1_6
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1_6
{
class Homework1_6
{
static void Main(string[] args)
{
int day;
int dayEnd;
while(true)
{
Console.WriteLine("請輸入起始年");
int year = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入起始月");
int month = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入起始日");
int date = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入結束年");
int yearEnd = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入結束月");
int monthEnd = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入結束日");
int dateEnd = int.Parse(Console.ReadLine());
if (year <= 0 || month <= 0 || date <= 0 || yearEnd <= 0 || monthEnd <= 0 || dateEnd <= 0)
{
break;
}
day = 0;
dayEnd=0;
for (int i = 1; i <= month; i++) //計算日期為起始年第幾天
{
if (i == month)
{
day += date;
break;
}
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
day += 31;
else if (i == 4 || i == 6 || i == 9 || i == 11)
day += 30;
else
{
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
day += 29;
else
day += 28;
}
}
for (int i = 1; i <= monthEnd; i++) //計算日期為結束年第幾天
{
if (i == monthEnd)
{
dayEnd += dateEnd;
break;
}
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
dayEnd += 31;
else if (i == 4 || i == 6 || i == 9 || i == 11)
dayEnd += 30;
else
{
if (yearEnd % 400 == 0 || (yearEnd % 4 == 0 && yearEnd % 100 != 0))
dayEnd += 29;
else
dayEnd += 28;
}
}
if(year==yearEnd) //若年份相同 則天數相減
{
Console.WriteLine("之間有{0}天",dayEnd-day-1);
}
if(year!=yearEnd) //若年份不同 加上之間的年天數
{
for (int i = year; i < yearEnd; i++)
{
if (i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))
dayEnd += 366;
else
dayEnd += 365;
}
Console.WriteLine("之間有{0}天", dayEnd - day - 1);
}
Console.ReadKey();
Console.Clear();
}
}
}
}
if 條件完 後述的break換行比較好
回覆刪除六題的namespace都不要用底槓
1_6 14, 28,31,34行命名不要有底槓
1_6 如果條件過多的if 如45行 可以考慮使用switch迴圈 增加程式頁面的整潔性
Homework1_1 題目命名不用underscore
回覆刪除Homework1_6 28 31 34行的變數命名不用underscore
1-5HW 45要換行
回覆刪除Homework1_3 沒有設定月份或天數大於12或31的條件
回覆刪除1_2~1_6有用到while無限迴圈
回覆刪除1_2 18行
1_3 18 21行
1_4 18 21 24行
1_5 18 21 24行
1_6 18 19 22 25 28 31 34 行
不要在迴圈之內宣告變數
1-1HW 17行可以和14行結合
回覆刪除不能結合,會在迴圈裡面重複宣告。
刪除用while(true)的話,建議再加上可以跳出迴圈的機制會比較好,不然使用者只能強制關閉程式才能離開.
回覆刪除