当前位置:文档之家› 西南大学荣昌校区2014级c#实验题

西南大学荣昌校区2014级c#实验题

1、计算9+99+999+……+9999…99(10个)的值。

staticvoid Main(string[] args)

{

int i;

double t = 0, s = 0;

for (i = 0; i< 10; i++)

{

t = t * 10 + 9;

s += t;

}

Console.WriteLine("s=" + s);

Console.ReadLine();

}

2、计算1+1/3+1/5+…+1/51的值,并显示出来。

staticvoid Main(string[] args)

{

double sum = 0;

for (double i = 1; i<= 51; i += 2)

sum += 1 / i;

Console.WriteLine(sum);

Console.ReadLine();

}

3、从键盘连续输入N 个正整数求其平均值。

static void Main(string[] args)

{

OperateNum();

}

static void OperateNum()

{

Console.WriteLine("请输入所需求的正整数的个数:");

string n1= Console.ReadLine();//读的是一个string类型

int n = Convert.ToInt32(n1);

int sum = 0;

double are;

int[] str = new int[n];

for(inti=0,j=i+1;i

{

Console.WriteLine("请输入第" + j + "个正整数: ");

str[i] = Int32.Parse(Console.ReadLine());//将控制台读入的数据出存到一个数组中

j++;//少了这句,则提醒输入的个数永远是1

}

{

sum += str[i];

}

double m= Convert.ToDouble(n);

are = sum /m;

Console.WriteLine("你输入的平均数为:" + are);

Console.ReadKey();

}

4、将数组{1,2,3,4,5,6,7,8,9,10}的值以每行两个输出。

staticvoid Main(string[] args)

{

int[] array = newint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i = 0; i

{

Console.Write(array[i] + "\t");

if (i % 2 == 1)

{

Console.WriteLine();

}

}

Console.ReadKey();

}

5、用循环和多分支选择结构分别计算数组{1,2,3,4,5,6,7,8,9}的奇数和、偶数和的值。

5.1 staticvoid Main(string[] args)

{

int[] str;

str = newint[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };//初始化数组

int i, sum1 = 0, sum2 = 0;

{

if (i % 2 == 0)

sum1 += str[i];

else

sum2 += str[i];

}

Console.WriteLine("奇数和为: " + sum1);

Console.WriteLine("偶数和a: " + sum2);

Console.ReadLine();

}

5.2. int[] values = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int sum1 = 0, sum2 = 0;

foreach (int value in values)

{

if (value % 2 == 0)

sum1 += value;

else

sum2 += value;

}

Console.WriteLine("偶数和: " + sum1);

Console.WriteLine("奇数和: " + sum2);

Console.ReadKey();

6、设计一个公共的person类

(1)该类包含:xh、xm、sex、age四个属性和Xh字段,其中xh属性为私有,Xh字段用来存取“xh”属性的值。

(2)该类包含两个构造方法,其中一个无参的构造方法,该构造方法什么都不做,另一个构造方法包含四个参数:string xh、string xm、string sex、int age。该构造方法用来初始化person类的实例。

(3)该类包含一个名为displayInf的普通方法,该方法返回为空,没有形式参数,用来输出学号、姓名、性别、年龄等属性的值。

(4)编写一个yjs类,该类由person类派生,yjs类中新增一个“所在院系”属性。yjs类包含一个构造方法,该构造方法包含五个参数,string xh、string xm、string sex、intage、string szyx;该构造方法的前四个参数调用person类的构造方法。

public class Person

{

private string xh;

public string Xh

{

get{ return xh;}

set{ xh = value; }

}

public string Xm { get; set; }

public string Sex { get; set; }

public int Age{ get; set; }

// 无参数的构造方法

public Person()

{

this.Xh = string.Empty;

this.Xm = string.Empty;

this.Sex = string.Empty;

this.Age = 0;

}

// 有参数的构造函数

public Person(string xh, string xm, string sex, int age)

{

this.Xh = xh;

this.Xm = xm;

this.Sex = sex;

this.Age = age;

}

public void DisplayInfo()

{

Console.WriteLine("学号:{0}, 姓名:{1}, 性别:{2}, 年龄:{3}",

this.Xh, this.Xm, this.Sex, this.Age);

}

}

7、从键盘输入一个数字,判断奇偶性。

using System;

usingSystem.Collections.Generic;

usingSystem.Text;

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

int t = Convert.ToInt32(Console.ReadLine()); ‘将指定的值转换为代正负号的32位元的整数’

if (t % 2 == 0)

Console.WriteLine("输入得数是偶数");

else

Console.WriteLine("输入得数是奇数");

Console.ReadLine();

}

}

}

8、判断从键盘中输入字符串首字符是数字,字母,或其他字符

using System;

usingSystem.Collections.Generic;

usingSystem.Text;

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("请输入字符串");

string s = Console.ReadLine();

char[] c = s.ToCharArray();//将s字符串中的内容复制到字符串数组中

if (c[0] >= '0' && c[0] <= '9')

Console.WriteLine("首字符是数字");

else

if ((c[0] >= 'a' && c[0] <= 'z') || (c[0] >= 'A' && c[0] <= 'Z'))

Console.WriteLine("首字符是字母");

else

Console.WriteLine ("首字符为其他");

Console.ReadLine();

}

}

}

9、输入两个数和运算符,根据运算符进行相应运算,并输出结果

using System;

usingSystem.Collections.Generic;

usingSystem.Text;

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

string op;

double d1, d2, result = 0;

Console.WriteLine("请输入第一个操作数");

d1 = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("请输入运算符");

op = Console.ReadLine();

Console.WriteLine("请输入第二个操作数");

d2 = Convert.ToDouble(Console.ReadLine());

switch (op)

{

case "+": result = d1 + d2; break;

case "-": result = d1 - d2; break;

case "*": result = d1 * d2; break;

case "/": result = d1 / d2; break;

default: Console.WriteLine("输入不合法的运算符"); break;

}

Console.WriteLine("{0}{1}{2}={3}", d1, op, d2, result);

Console.ReadLine();

}

}

}

10、猜字游戏

class Program

{

static void Main(string[] args)

{

Random ra = new Random();

intrndint = ra.Next(1, 100);

Console.WriteLine("请输入一个整数(范围1~100)"); Console.WriteLine("如果要退出请输入0键"); intinputint = int.Parse(Console.ReadLine());

l1: if(inputint>=0&inputint<=100)

{

while(!(inputint==0))

{

if(inputint==rndint)

{

Console.WriteLine("恭喜你,猜对啦!");

Console.WriteLine("继续输入Y,退出输入N"); stringinputnext = Console.ReadLine();

if(inputnext=="Y")

{

rndint=ra.Next(1,100);

}

else

return;

}

else if(inputint

Console.WriteLine("你猜小啦");

else

Console.WriteLine("你猜大啦");

inputint=System.Convert.ToInt32(Console.ReadLine());

}

if(inputint==0)

return;

}

else

{

Console.WriteLine("你的输入有误,请输入一个整数"); inputint=int.Parse (Console.ReadLine ());

goto l1;

}

}

}

}

11、声明一个数组,将一年十二个月的英文存入其中。当用户输入月份时打印出相应的英文。如果输入0则退出

static void main(string[ ] args)

{

string[ ] months={"ja "," fe"," ma"," ap","may ","jue "," july","au ","sep ",”oct “ ,"nov" ,"dec"};

console.writeline(" qing输入月份,如果输入0则退出,(输入2显示feb)"); int m=convert.toint32(console.readline());

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

console.writeline(months[m-1];

else if(m==0)

return

else

console.writeline("输入月份有错");

console.readline();

}

12、统计各分数段学生的人数和百分比,已知某班10个学生c#成绩80,65,67,90,76,24,56,88,99,90,统计优秀,良好,及格,中等,不及格所占百分比

static void main(string[ ] args)

{

int [ ] score={80,65,67,90,76,24,56,88,99,90};

int a=0,b=0,c=0,d=0,e=0;

foreach(int s in score)

{

if(s>=90&&s<=100)

a++;

else if(s>=80&&s<=89)

b++;

else if(s>=70&&s<=79)

c++;

else if(s>=60&&s<=69)

d++;

else

if(s<60)

e++;

}

console.writeline("优秀人数{ 0},百分比{1}",a,a/10.0);

......

.....

.....

console.readline(); }

相关主题
文本预览
相关文档 最新文档