实验报告2 创建简单的C#应用程序及基本编程方法

更新时间:2024-07-10 11:22:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验名称:创建简单的C#应用程序及基本编程方法

一、实验目的:

(1)了解C# 的数据类型

(2)掌握各种变量的声明方式。 (3)理解运算符的优先级。

(4)掌握C#简单数据类型、运算符与表达式、数组的使用方法。

(5)理解C# 程序语法结构,掌握顺序结构、选择结构和循环结构语法的程序设计方法。 (6)通过以上内容,掌握C# 语言的编程规则。

二、上机内容:

(1)编写一个声明C#不同数据类型变量的程序。 (2)编写一个使用运算符、表达式、变量的程序。 (3)编写一个使用C#数组的的程序。 (4)编写使用不同选择结构的程序。 (5)编写使用不同循环结构结构的程序。

三、上机原理、方法和手段:

本上机练习C#的主要数据类型,原则主要C#中数据类型在应用时应该先进行变量的定义才可以使用,在C#中的数组及应用,以及使用数据进行表达式的计算。利用条件语句来实现功能选择功能,利用循环语句来解决重复计算要求的问题,设计程序并调试运行。

四、上机步骤:

(1)编程求一个一维和二维整数数组的最大值、最小值、平均值和所有数组元素的和。

一维数组代码:using System;

using System.Collections.Generic; using System.Text; namespace shiyan2 {

class Program {

static void Main(string[] args) {

Console.WriteLine(\请输入数组元素个数\ int a = int.Parse(Console.ReadLine()); int[] b = new int[a]; int sum = 0; int ave = 0;

for (int i = 0; i < a; i++) {

Console.WriteLine(\请输入第{0}个数\ b[i] = int.Parse(Console.ReadLine()); sum = sum + b[i];

}

ave = sum / a; int max = b[0]; int min = b[0];

for (int j = 0; j < a; j++) {

if (b[j] < min) {

min = b[j]; }

if (b[j] > max) {

max = b[j]; }

}

Console.WriteLine(\ Console.WriteLine(\ Console.WriteLine(\ Console.WriteLine(\ Console.Read(); } }

}运行结果如右图:

二维数组代码:using System;

using System.Collections.Generic; using System.Text;

namespace shuzu2 {

class Program {

static void Main(string[] args) {

Console.WriteLine(\请输入数组的行数\); int a = int.Parse(Console.ReadLine()); Console.WriteLine(\请输入数组的列数\); int b = int.Parse(Console.ReadLine()); int[,] d = new int[a, b]; int c = a * b; int sum = 0;

int ave = 0;

for (int i = 0; i < b; i++) {

for (int j = 0; j < a; j++) {

Console.WriteLine(\请输入第{0}行第{1}列的数\, i + 1, j + 1); d[i, j] = int.Parse(Console.ReadLine()); sum = sum + d[i, j];

} }

ave = sum / c; int max = d[0, 0]; int min = d[0, 0];

for (int m = 0; m < a; m++) {

for (int t = 0; t < b; t++) {

if (max < d[m, t]) { max = d[m, t]; } if (min > d[m, t]) { min = d[m, t]; } } }

Console.WriteLine(\, sum); Console.WriteLine(\ + ave); Console.WriteLine(\, max); Console.WriteLine(\, min); Console.ReadLine(); } }

}运行结果如图:

(2)利用ArrayList编写一个简单的统计程序,能够统计出一组数据均值、方差、2阶原点矩、2阶中心距、标准差、极值、极差等数字特征。

using System;

using System.Collections;

using System.Text;

namespace arraylistexample {

class Program {

static void Main(string[] args) {

ArrayList myal = new ArrayList(); double sum = 0;

int i = int.Parse(Console.ReadLine()); while (i > 0) {

myal.Add(i);

i = int.Parse(Console.ReadLine()); }

double ave=0.0; int max, min;

max = (int)myal[0]; min = (int)myal[0]; int jc = max - min; double bzc;

double y=0.0;double z=0.0; double fc = 0; double s = 0;

foreach (int j in myal) {

sum = sum + j; if (max < j) { max = j; } if (min > j) { min = j; } }

ave = sum / (myal.Count); jc = max - min;

foreach (int j in myal) {

s = s + Math.Pow((j - ave), 2); y = y + Math.Pow(j, 2);

z = z + Math.Pow((j - ave), 2); }

fc = s / myal.Count; bzc = Math.Pow(fc, 0.5); y = y / myal.Count; z = z / myal.Count;

Console.WriteLine(\,sum={1},ave={2},fc={3},bzc={4},z={5}\, y, sum, ave, fc, bzc,z); Console.ReadLine(); } } }

运行结果如下

编写一个数据类型转换的程序,能够完成C#提供的4种数据类型转换方式,并进行+、-、×、/、%运算。

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication13 {

class Program {

static void Main(string[] args) {

double i = Convert.ToDouble(Console.ReadLine()); int j = (int)(i / 2);

string c = i.ToString() + j.ToString(); long m = j % 3; long a = j * m; double b = i - j;

Console.WriteLine(\结果为:i=\ + i + \ + \ + j + \ + \ + c + \ + \ +

m + \ + \ + a + \ + \ + b + \); Console.ReadLine(); } } }

使用if...else 语句描述下面的流程图,判定结果自己设定。

开始输入数据1输入数据2数据1>数据2?否数据1=数据2?否是输出判定结果1是输出判定结果2输出判定结果3

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication8 {

class Program {

static void Main(string[] args) {

Console.WriteLine(\开始\);

Console.WriteLine(\输入数据一:\); int a = int.Parse(Console.ReadLine()); Console.WriteLine(\输入数据二:\); int b = int.Parse(Console.ReadLine()); if (a > b) {

Console.WriteLine(\输出判定结果一:\); Console.WriteLine(\数据一大于数据二\); }

else if (a == b) {

Console.WriteLine(\输出判定结果二:\);

Console.WriteLine(\数据一等于数据二\); } else {

Console.WriteLine(\输出判定结果三:\); Console.WriteLine(\数据一小于数据二\); } Console.ReadLine(); } }

}运行结果如右图:

使用switch语句设计一个简单的星期提示程序,根据用户从命令行输入的数据输出相应的星期提示,比如,当输入1的时候,则提示“今天是周一”,依此类推。

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication6 {

class Program {

static void Main(string[] args) { {

Console.WriteLine(\请输入一个数字:\); string i = Console.ReadLine();

bool flag = false;

do {

switch (i) {

case \:

Console.WriteLine(\今天是周一\); flag = true; break; case \:

Console.WriteLine(\今天是周二\); flag = true; break; case \:

Console.WriteLine(\今天是周三\); flag = true; break; case \:

Console.WriteLine(\今天是周四\); flag = true; break; case \:

Console.WriteLine(\今天是周五\); flag = true; break; case \:

Console.WriteLine(\今天是周六\); flag = true; break; case \:

Console.WriteLine(\今天是周日\); flag = true; break;default:

Console.WriteLine(\输入的数据不对,请重新输入\); flag = true; break; }

i = Console.ReadLine();

} while (flag != false&&i!=\); Console.ReadLine(); } }

}

}运行结果如右图:

使用三元运算符“?:”和for 循环创建一个控制台程序,用以判定用户从命令行输入的某一年份是否为闰年。

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication9 {

class Program {

static void Main(string[] args) {

Console.WriteLine(\请输入一个年份:\); int i=int.Parse(Console.ReadLine()); do{

string message = (((i % 4 == 0) && (i % 100 != 0)) ||(i % 400 == 0)) ? \是闰年!\ : \不是闰年!\;

Console.WriteLine(i + message); i = int.Parse(Console.ReadLine()); } while (i != 0); Console.ReadLine(); } }

}运行结果如下图:

分别使用while和do?while编写一个求的阶乘的程序,由用户从命令行输入。

(1)while

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication10 {

class Program {

static void Main(string[] args)

{//分别使用while和do…while编写一个求n的阶乘的程序, 由用户从命令行输入。 int n = int.Parse(Console.ReadLine()); int f = 1; while (n != 0) {

f = f * n; n = n - 1; }

Console.WriteLine(\的阶乘为:\+f); Console.ReadLine(); } }

}运行结果如右图: (2)do…while

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication11 {

class Program {

static void Main(string[] args) {

int n = int.Parse(Console.ReadLine()); long s = 1; do {

s = s * n; n = n - 1; }while(n!=0);

Console.WriteLine(\的阶乘为:\+s); Console.ReadLine(); } } }

运行结果如右图:

采用多重循环编写一个输出九九乘法表的程序。 using System;

using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication12 {

class Program {

static void Main(string[] args)

{//采用多重循环编写一个输出九九乘法表的程序。 Console.WriteLine(\九九乘法表\); for (int i = 1; i < 10; i++) {for( int j=1;j<=i;j++) {

int s = i * j;

Console.Write(i+\+j+\+s+\);

}

Console.WriteLine(); }

Console.ReadLine(); } }

}运行结果如下:

本文来源:https://www.bwwdw.com/article/3jt.html

Top