日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

20道C#练习题(一)1——10题

發(fā)布時(shí)間:2023/11/30 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 20道C#练习题(一)1——10题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.輸入三個(gè)整數(shù),xyz,最終以從小到大的方式輸出。利用if嵌套。

???????????? Console.Write("請(qǐng)輸入x=");

??????????? double x = double.Parse(Console.ReadLine());

??????????? Console.Write("請(qǐng)輸入y=");

??????????? double y = double.Parse(Console.ReadLine());

??????????? Console.Write("請(qǐng)輸入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.輸入三個(gè)整數(shù),xyz,最終以從小到大的方式輸出。利用中間變量。

????????? Console.Write("請(qǐng)輸入x=");

??????????? x = double.Parse(Console.ReadLine());

??????????? Console.Write("請(qǐng)輸入y=");

??????????? y = double.Parse(Console.ReadLine());

??????????? Console.Write("請(qǐng)輸入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.輸入三個(gè)整數(shù),xyz,最終以從小到大的方式輸出。利用條件運(yùn)算符。

????????? ?Console.Write("請(qǐng)輸入x=");

??????????? double x = double.Parse(Console.ReadLine());

??????????? Console.Write("請(qǐng)輸入y=");

??????????? double y = double.Parse(Console.ReadLine());

??????????? Console.Write("請(qǐng)輸入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.“現(xiàn)在幾點(diǎn)了?”鍵盤(pán)鍵入小時(shí)數(shù),判斷是上午還是下午。打印出來(lái)現(xiàn)在是上午幾點(diǎn)還是下午幾點(diǎn)。利用條件運(yùn)算符。

Console.Write("現(xiàn)在幾點(diǎn)了?");

??????????? int a = int.Parse(Console.ReadLine());

??????????? string b=a>12?(a-12)+"pm":a+"am";

??????????? Console.WriteLine("現(xiàn)在是"+b);

5.相親過(guò)程:你有房子么?你有錢(qián)么?你有能力么?

【結(jié)婚吧】【先買(mǎi)房子在結(jié)婚】【先賺錢(qián)再買(mǎi)房子再結(jié)婚】都沒(méi)有【拜拜~~】

利用if嵌套做相親過(guò)程。

??????????? Console.WriteLine("你有房子嗎?");

??????????? string a = Console.ReadLine();

??????????? if (a == "有")

??????????? {

??????????????? Console.WriteLine("結(jié)婚吧?");

??????????? }

??????????? else

??????????? {

??????????????? Console.WriteLine("你有錢(qián)嗎?");

??????????????? string b = Console.ReadLine();

??????????? ????if (b == "有")

??????????????? {

??????????????????? Console.WriteLine("先買(mǎi)房在結(jié)婚。");

??????????????? }

??????????????? else

??????????????? {

??????????????????? Console.WriteLine("你有能力嗎?");

??????????????????? string c = Console.ReadLine();

??????????????????? if (c == "有")

??????????????????? {

??????????????????????? Console.WriteLine("先賺錢(qián)再買(mǎi)房再結(jié)婚。");

??????????????????? }

??????????????????? else

??????????????????? {

??????????????????????? Console.WriteLine("拜拜!");

??????????????????? }

??????????????? }

??????????? }

?

6.輸入年月日,看看格式是否正確。利用if嵌套。

????????? Console.Write("請(qǐng)輸入年份:");

????????? ??int y = int.Parse(Console.ReadLine());

??????????? if (y >= 0 && y <= 9999)

??????????? {

??????????????? Console.Write("請(qǐng)輸入月份:");

??????????????? int m = int.Parse(Console.ReadLine());

??????????????? if(m>=1&&m<=12)

??????????????? {

????????????????? ??Console.Write("請(qǐng)輸入日期:");

??????????????????? 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("請(qǐng)輸入年月日(****/**/** **;**;**)");

??????????? try

??????????? {

??????????????? DateTime shijian = DateTime.Parse(Console.ReadLine());

??????????????? Console.WriteLine("格式正確,你輸入的是:" + shijian);

??????????? }

??????????? catch

??????????? {

???????? ???????Console.WriteLine("你輸入的格式有誤。");

??????????? }

?

8.做人機(jī)猜拳,剪刀石頭布。利用switch case。

???????? int fenshu = 0;

??????????? for (; ; )

??????????? {

??????????????? Console.WriteLine("猜拳游戲:");

??????????????? Console.WriteLine("1、剪刀");

??????????????? Console.WriteLine("2、包袱");

??????????????? Console.WriteLine("3、錘");

??????????????? Console.WriteLine("4、結(jié)束");

??????????????? 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("請(qǐng)按回車(chē)鍵繼續(xù)。");

??????????????????? Console.ReadLine();

??????????????? }

??????????????? else

??????????????? {

??????????????????? if (a == 4)

??????????????????? {

??????????????????????? break;

??????????????????? }

??????????????????? Console.WriteLine("輸入有誤,請(qǐng)重新輸入");

??????????????? }

??????????? }

9.輸入一個(gè)正整數(shù),求1!+2!+3!+...+n!。利用for循環(huán)嵌套。

???????? Console.Write("請(qǐng)輸入正整數(shù)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以內(nèi)與7有關(guān)的數(shù)并打印,并求出他們的和。利用for循環(huán)+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));

轉(zhuǎn)載于:https://www.cnblogs.com/zst062102/p/5292321.html

總結(jié)

以上是生活随笔為你收集整理的20道C#练习题(一)1——10题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。