实验二 C#编程基础(含答案)

实验二 C#编程基础(含答案)

ID:44819700

大小:141.00 KB

页数:9页

时间:2019-10-30

实验二 C#编程基础(含答案)_第1页
实验二 C#编程基础(含答案)_第2页
实验二 C#编程基础(含答案)_第3页
实验二 C#编程基础(含答案)_第4页
实验二 C#编程基础(含答案)_第5页
资源描述:

《实验二 C#编程基础(含答案)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、实验二C#编程基础1整数排序参考答案:3_1代码如下://Test3_1.csusingSystem;classTest3_1{staticvoidMain(){inta,b,c,t;Console.Write("请输入第一个整数:");a=Int32.Parse(Console.ReadLine());Console.Write("请输入第二个整数:");b=Int32.Parse(Console.ReadLine());Console.Write("请输入第三个整数:");c=Int32.Parse(Console.ReadLine());if(a>b){t=a;a=b;b=t;}if(b

2、>c){t=c;c=b;if(t>a)b=t;else{b=a;a=t;}}Console.WriteLine("从小到大的顺序依次为:{0},{1},{2}",a,b,c);}运行结果如图:图3-12简单计算器编写一个简单的计算器程序,能够根据用户从键盘输入的运算指令和整数,进行简单的加减乘除运算。参考答案:3_2的代码如下://Test3_2.csusingSystem;classtest3_2{staticvoidMain(){//声明两个输入的操作数。intfirstNumber,secondNumber;//声明一个字符串类型来定义运算符。stringoperation;//来判断是

3、否继续运算。stringresponse;do{Console.Write("请输入运算符(+、-、*、/):");operation=Console.ReadLine();Console.Write("请输入第一个操作数:");//接收输入的操作数并转换成整形。firstNumber=Int32.Parse(Console.ReadLine());Console.Write("请输入第二个操作数:");secondNumber=Int32.Parse(Console.ReadLine());switch(operation){case"+":Console.WriteLine("{0}+{1

4、}={2}",firstNumber,secondNumber,firstNumber+secondNumber);break;case"-":Console.WriteLine("{0}-{1}={2}",firstNumber,secondNumber,firstNumber-secondNumber);break;case"*":Console.WriteLine("{0}*{1}={2}",firstNumber,secondNumber,firstNumber*secondNumber);break;case"/":Console.WriteLine("{0}/{1}={2}",fi

5、rstNumber,secondNumber,firstNumber/secondNumber);break;default:Console.WriteLine("运算符不合法");break;}Console.Write("是否继续进行运算(Y/N);");response=Console.ReadLine();while(response!="Y"&&response!="y"&&response!="N"&&response!="n"){Console.WriteLine("输入错误!");Console.Write("是否继续进行运算(Y/N):");response=Console.

6、ReadLine();}}while(response=="Y"

7、

8、response=="y");}}运行结果如下图:图3-23求指定范围内的所有合数合数就是非素数,即除了1和它本身之外还有其他约数的正整数。编写一个程序求出指定数据范围(假设10~100)内的所有合数。参考答案://Test3_3.csusingSystem;classTest3_3{staticvoidMain(){intmin=10,max=100;intnum=0;Console.WriteLine("{0}到{1}之间的合数有:",min,max);for(inti=min;i<=max;i++){//for(intj

9、=2;j

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。