1.輸入三個整數,xyz,最終以從小到大的方式輸出。利用if嵌套。
Console.Write("請輸入x=");
double x = double.Parse(Console.ReadLine());
Console.Write("請輸入y=");
double y = double.Parse(Console.ReadLine());
Console.Write("請輸入z=");
double z = double.Parse(Console.ReadLine());
if (x < y && x < z)
{
Console.WriteLine(x);
if(y<z)
{
Console.WriteLine(y);
Console.WriteLine(z);
}
else
{
Console.WriteLine(z);
Console.WriteLine(y);
}
}
else if (y < x && y < z)
{
Console.WriteLine(y);
if(x<z)
{
Console.WriteLine(x);
Console.WriteLine(z);
}
else
{
Console.WriteLine(z);
Console.WriteLine(x);
}
}
else//z最小
{
Console.WriteLine(z);
if(x<y)
{
Console.WriteLine(x);
Console.WriteLine(y);
}
else
{
Console.WriteLine(y);
Console.WriteLine(x);
}
}
2.輸入三個整數,xyz,最終以從小到大的方式輸出。利用中間變量。
Console.Write("請輸入x=");
x = double.Parse(Console.ReadLine());
Console.Write("請輸入y=");
y = double.Parse(Console.ReadLine());
Console.Write("請輸入z=");
z = double.Parse(Console.ReadLine());
double zhong;
if(x<y&&x<z)
{
if (y < z) { }
else
{
zhong = y; y = z; z = zhong;
}
}
else if (y < x && y < z)
{
zhong = x; x = y; y = zhong;//x<y&&x<z
if (y < z) { }
else
{
zhong = y; y = z; z = zhong;
}
}
else //z最小
{
zhong = x; x = z; z = zhong;//x<y&&x<z
if (y < z) { }
else
{
zhong = y; y = z; z = zhong;
}
}
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(z);
3.輸入三個整數,xyz,最終以從小到大的方式輸出。利用條件運算符。
Console.Write("請輸入x=");
double x = double.Parse(Console.ReadLine());
Console.Write("請輸入y=");
double y = double.Parse(Console.ReadLine());
Console.Write("請輸入z=");
double z = double.Parse(Console.ReadLine());
min = x > y ? (y > z ? z : y) : (x > z ? z : x);
zhong = x > y ? (y > z ? y : (x>z?z:x)) : (x > z ? x : (y>z?z:y));
max = x > y ? (x > z ? x : z) : (y > z ? y : z);
Console.WriteLine(min);
Console.WriteLine(zhong);
Console.WriteLine(max);
4.“現在幾點了?”鍵盤鍵入小時數,判斷是上午還是下午。打印出來現在是上午幾點還是下午幾點。利用條件運算符。
Console.Write("現在幾點了?");
int a = int.Parse(Console.ReadLine());
string b=a>12?(a-12)+"pm":a+"am";
Console.WriteLine("現在是"+b);
5.相親過程:你有房子么?你有錢么?你有能力么?
【結婚吧】【先買房子在結婚】【先賺錢再買房子再結婚】都沒有【拜拜~~】
利用if嵌套做相親過程。
Console.WriteLine("你有房子嗎?");
string a = Console.ReadLine();
if (a == "有")
{
Console.WriteLine("結婚吧?");
}
else
{
Console.WriteLine("你有錢嗎?");
string b = Console.ReadLine();
if (b == "有")
{
Console.WriteLine("先買房在結婚。");
}
else
{
Console.WriteLine("你有能力嗎?");
string c = Console.ReadLine();
if (c == "有")
{
Console.WriteLine("先賺錢再買房再結婚。");
}
else
{
Console.WriteLine("拜拜!");
}
}
}
6.輸入年月日,看看格式是否正確。利用if嵌套。
Console.Write("請輸入年份:");
int y = int.Parse(Console.ReadLine());
if (y >= 0 && y <= 9999)
{
Console.Write("請輸入月份:");
int m = int.Parse(Console.ReadLine());
if(m>=1&&m<=12)
{
Console.Write("請輸入日期:");
int d = int.Parse(Console.ReadLine());
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
if(d>=1&&d<=31)
{
Console.WriteLine("格式正確,你輸入的是"+y+"年"+m+"月"+d+"日。");
}
else
{
Console.WriteLine("你輸入日期格式有誤。");
}
}
else if (m == 4 || m == 6 || m == 9 || m == 11)
{
if (d >= 1 && d <= 30)
{
Console.WriteLine("格式正確,你輸入的是" + y + "年" + m + "月" + d + "日。");
}
else
{
Console.WriteLine("你輸入日期格式有誤。");
}
}
else//m==2
{
if(y%4==0&&y%100!=0||y%400==0)
{
if (d >= 1 && d <= 29)
{
Console.WriteLine("格式正確,你輸入的是" + y + "年" + m + "月" + d + "日。");
}
else
{
Console.WriteLine("你輸入日期格式有誤。");
}
}
else
{
if (d >= 1 && d <= 28)
{
Console.WriteLine("格式正確,你輸入的是" + y + "年" + m + "月" + d + "日。");
}
else
{
Console.WriteLine("你輸入日期格式有誤。");
}
}
}
}
else
{
Console.WriteLine("你輸入的月份格式有誤。");
}
}
else
{
Console.WriteLine("你輸入的年份格式有誤。");
}
7.輸入年月日,看看格式是否正確。利用DateTime。
Console.Write("請輸入年月日(****/**/** **;**;**)");
try
{
DateTime shijian = DateTime.Parse(Console.ReadLine());
Console.WriteLine("格式正確,你輸入的是:" + shijian);
}
catch
{
Console.WriteLine("你輸入的格式有誤。");
}
8.做人機猜拳,剪刀石頭布。利用switch case。
int fenshu = 0;
for (; ; )
{
Console.WriteLine("猜拳游戲:");
Console.WriteLine("1、剪刀");
Console.WriteLine("2、包袱");
Console.WriteLine("3、錘");
Console.WriteLine("4、結束");
Console.Write("你要出的是:");
int a = int.Parse(Console.ReadLine());
Random ran = new Random();
int n = ran.Next(1, 4);
if (a >= 1 && a <= 3)
{
switch (n)
{
case 1:
Console.WriteLine("電腦出:剪刀");
break;
case 2:
Console.WriteLine("電腦出:包袱");
break;
case 3:
Console.WriteLine("電腦出:錘");
break;
}
if(a-n==2||a-n==-1)
{
fenshu++;
Console.WriteLine("你贏了!");
Console.WriteLine("得分為:"+(fenshu));
}
else if(a-n==-2||a-n==1)
{
fenshu--;
Console.WriteLine("電腦贏了!");
Console.WriteLine("得分為:"+(fenshu));
}
else
{
Console.WriteLine("打平了!");
Console.WriteLine("得分為:" + (fenshu));
}
Console.WriteLine("請按回車鍵繼續。");
Console.ReadLine();
}
else
{
if (a == 4)
{
break;
}
Console.WriteLine("輸入有誤,請重新輸入");
}
}
9.輸入一個正整數,求1!+2!+3!+...+n!。利用for循環嵌套。
Console.Write("請輸入正整數n=");
int n = int.Parse(Console.ReadLine());
sum = 0;
for (int i = 1; i <= n;i++ )
{
int sum1=1;
for (int j = 1; j <= i;j++ )
{
sum1 = sum1 * j;
}
sum = sum + sum1;
}
Console.WriteLine("階乘和:" + sum);
10.找出100以內與7有關的數並打印,並求出他們的和。利用for循環+if。
int sum = 0;
for (int i = 0;i<=100;i++ )
{
if(i%7==0||i%10==7||i/10==7)
{
Console.WriteLine(i);
sum = sum + i;
}
}
Console.WriteLine("總和為:"+(sum));