**
课程名称 .NET程序设计 实验名称 实验一 C#基本编程方法
**
一.实验目的
1.练习C#中变量声明和赋值的方法。
2.练习C#类型转换的方法。
3.练习C#分支语句的基本用法。
4.练习C#循环语句的基本用法。
二.实验内容
1.编写并调试一个C#控制台应用程序,输出1到5的平方值。
2.编写一个C#控制台应用程序,要求完成下列功能。
(1)接收一个整数n。
(2)如果接收的值n为正数,输出1到n间的全部整数,循环转向(1),继续接收一个整数n。
(3)如果接收的值为负值,用break退出循环。
3.为铁路部门编写计算运费的程序。假设铁路托运行李,规定每张客票托运费计算方法是:行李重量不超过50kg时,每千克0.25元;超过50kg而不超过100kg时,其超出部分每千克0.35元时;超过100kg时,其超过部分每千克0.45元。要求从键盘上输入行李重量后,程序计算并输出托运的费用。
三、实验代码
1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
Console.Write(i * i + " ");
}
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class 实验一_2_
{
static void Main(string[] args) {
while (true)
{
Console.Write("请输入一个整数:");
int a = Convert.ToInt32(Console.ReadLine());
if (a <= 0)
{
Console.WriteLine("请输入大于0的整数。");
}
else
{
double b = 0;
for (int c = 1; c <= a; c++)
{
Console.WriteLine("结果:{0}", c);
}
Console.WriteLine("按回车后可继续输入。");
}
Console.ReadLine();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
double w,x=0;
Console.Write("请输入重量");
w = double.Parse(Console.ReadLine());
if (w<= 50)
x=0.25*w;
if (w>50 &&w <= 100)
x=0.25*50+0.35*(w-50);
if (w>100)
x=0.25*50+0.35*50+0.45*(w-100);
Console. WriteLine("运费为{0}",x) ;
Console.ReadLine();
}
}
}
四、实验结果
五、实验总结
通过此次实验学会了如何搭建C#的语言环境,如何创建一个网站和项目,知道了网站和项目的区别,并对网站中的标签和内容有了较为深刻的理解,能够自己创建一个简单的网站,并进行运行及调试。
解了C#语言的一些基本用法,并学习了C#循环语句的基本用法、类型转换、变量声明和赋值的方法,能够简单运用到程序中。
> 代码资源下载链接:https://download.csdn.net/download/XIARIANNUAN/13137610