文章目录
一、 实验目的及要求
- 熟悉Visual Studio编译环境。
- 掌握创建控制台应用程序的一般步骤。
- 初识C#程序结构,识记C#程序编写规范。
二、实验内容(或实验原理、实验拓扑)
- 让用户输入姓名 语文 数学 英语 三门课的成绩,然后给用户显示:XX,你的总成绩为XX分,平均成绩为XX分。(类型转换练习)
三、实验设备与环境
I7 7700HQ+16G内存+38TB分布式硬盘的笔记本电脑、Windows 10 1903政府版、Visual Studio 2019 企业版、.NET Framework 4.7.2
四、实验设计方案(包括实验步骤、设计思想、算法描述或开发流程等)
五、实验结果(包括设计效果、测试数据、运行结果等)
Input name:
Coco
Your name is Coco.
Input Chinese scores :
100
Your Chinese scores is 100.
Input Math scores :
99
Your Math scores is 99.
Input English scores :
99
Your English scores is 99.
Dear Coco, your total scores is 298, your average scores is 99.33.
Press any key to quit.
六、实验小结(包括收获、心得体会、注意事项、存在问题及解决办法、建议等)
C#中decimal ,double,float的区别
Name | CTS Type | Description | Significant Figures | Range (approximate) |
---|---|---|---|---|
float | System.Single | 32-bit single-precision floating point | 7 | ±1.5 × 10?45 to ±3.4 × 1038 |
double | System.Double | 64-bit double-precision floating point | 15/16 | ±5.0 × 10 ?324 to ±1.7 × 10308 |
decimal | System.Decimal | 128-bit high precision decimal notation | 28 | ±1.0 × 10?28 to ±7.9 × 1028 |
- 如果我们在代码中写一个12.3,编译器会自动认为这个数是个double型。所以如果我们想指定12.3为float类型,那么你必须在数字后面加上
F/f
(Decimal加M/m
)。 - decimal类型用来表示高精度的浮点数,从上表可以看出,decimal的有效位数很大,达到了28位,但是表示的数据范围却比float和double类型小。decimal类型并不是C#中的基础类型,所以使用的时候会对计算时的性能有影响。
定义一个decimal类型的浮点数:decimal d = 12.30M;
七、附录(包括作品、流程图、源程序及命令清单等)
本实验报告同步在:https://blog.csdn.net/COCO56/article/details/100633111
using System;
namespace Test1_2
{
class Program
{
static void Main(string[] args)
{
//让用户输入姓名 语文 数学 英语 三门课的成绩,然后给用户显示:XX,你的总成绩为XX分,平均成绩为XX分。(类型转换练习)
Console.WriteLine("Input name:");
string name = Console.ReadLine();
Console.WriteLine("Your name is " + name + ".");
Console.WriteLine("\nInput Chinese scores :");
decimal ChineseScores = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Your Chinese scores is " + ChineseScores + ".");
Console.WriteLine("\nInput Math scores :");
decimal MathScores = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Your Math scores is " + MathScores + ".");
Console.WriteLine("\nInput English scores :");
decimal EnglishScores = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Your English scores is " + EnglishScores + ".");
decimal TotalScores = ChineseScores + MathScores + EnglishScores;
decimal AverageScores = TotalScores / 3;
AverageScores = Math.Round(AverageScores, 2);
Console.WriteLine("\nDear {0}, your total scores is {1}, your average scores is {2}.", name, TotalScores, AverageScores);
Console.WriteLine("\nPress any key to quit.");
Console.ReadKey();
}
}
}