实验报告2 创建简单的C#应用程序及基本编程方法
更新时间:2024-07-10 11:22:01 阅读量: 综合文库 文档下载
- 实验报告200字推荐度:
- 相关推荐
实验名称:创建简单的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(); } }
}运行结果如下:
正在阅读:
如何指引社工对专业服务做反思02-23
固安县孔雀城莱茵河谷房价以及地理位置07-28
2018新部编本人教版语文三年级上册教材分析【三篇】08-28
大学体验英语第三册作业题及参考答案04-20
聚氯乙烯简介11-05
如何做一名优秀的教师02-22
财务人员廉政谈话记录范文财务人员廉政谈话内容记录(3篇)03-24
关于污水处理厂项目建议书01-03
走近春天作文450字07-01
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- C#
- 应用程序
- 创建
- 编程
- 实验
- 简单
- 基本
- 报告
- 方法
- 备战高考数学大二轮复习专题七概率与统计专题能力训练19排列、组
- 2018版中国家用燃气灶具行业现状调研分析报告目录
- 言语教学:追寻语感与语理的平衡-教育文档
- 2012高三数学寒假作业10(覃祖光编写)
- 2017年淄博市高三第二次调研考试生物试题
- 10kv及以下客户供用工程典型设计方案 - secret
- 新时期领导干部的素质和能力要求
- 电子商务交易诚信的博弈分析毕业论文
- 学生国庆演讲-党是我心中一颗最亮的星
- 华罗庚金杯数学邀请赛决赛初二组练习题(含答案)
- 中英文双语字幕电影下载网站一览
- 大班学期末家长会家长代表发言(精选多篇)-实用word文档
- 几百部英文电影mp3(附字幕 - 英语学习绝佳材料 - 强烈推荐)
- 2012届高考数学步步高第二轮复习训练:专题六第1讲 排列与组合、
- 江苏省南通中学(南区)高三数学寒假作业1 2012,1
- 五项管理(行动日志样本)
- 本科学年论文定稿
- 勾股定理的别名
- 上海市杨浦区2017届高三生物4月质量调研二模试题
- 周末作业10(三角函数的性质2)