实验二 C#编程基础
1 整数排序
参考答案:
3_1代码如下:
//Test3_1.cs using System; class Test3_1{ static void Main(){ int a,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>c){ t=c; c=b; if(t>a) b=t; else{ b=a; a=t; } } Console.WriteLine(\从小到大的顺序依次为:{0},{1},{2}\ }
运行结果如图:
图3-1
2 简单计算器
编写一个简单的计算器程序,能够根据用户从键盘输入的运算指令和整数,进行简单的加减乘除运算。
参考答案:
3_2的代码如下:
//Test3_2.cs using System; class test3_2{ static void Main(){ //声明两个输入的操作数。 int firstNumber,secondNumber; //声明一个字符串类型来定义运算符。 string operation; //来判断是否继续运算。 string response; 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(\ r+secondNumber); break; case\ Console.WriteLine(\secondNumber); break; case\ Console.WriteLine(\* secondNumber); break; case\ Console.WriteLine(\ } } econdNumber); break; default: Console.WriteLine(\运算符不合法\ break; } Console.Write(\是否继续进行运算(Y/N);\response=Console.ReadLine(); while(response!=\ Console.WriteLine(\输入错误!\ Console.Write(\是否继续进行运算(Y/N):\ response=Console.ReadLine(); } }while(response==\
运行结果如下图:
图3-2
3 求指定范围内的所有合数 合数就是非素数,即除了1和它本身之外还有其他约数的正整数。编写一个程序求出指定数据范围(假设10~100)内的所有合数。
参考答案:
//Test3_3.cs using System; class Test3_3{ static void Main(){ int min=10,max=100; int num=0; Console.WriteLine(\到{1}之间的合数有:\for(int i=min;i<=max;i++){ //for(int j=2;j
运行结果如图:
图3-3
4、浮点型数组排序
用户输入6个浮点型数组,程序根据其值的大小,进行排序输出。
using System; using System.Collections.Generic; using System.Text; namespace Example_4 { ///