11月11每日打卡
实验一 语言基础
一、实验目的
1. 熟悉Visual Stido.NET 实验环境;
2. 掌握控制台程序的编写方法;
3. 掌握C#程序设计语言的语法基础;
4. 掌握控制语句和数组的使用。
二、实验要求
根据题目要求,编写 C#程序,并将程序代码和运行结果写入实验报告。
三、实验内容
1. 编写一个控制台应用程序,输入三角形或者长方形边长,计算其周长和面积并输出。
using System;
class Program
{
static void Main()
{
Console.WriteLine("请选择要计算的形状(triangle或rectangle):");
string shape = Console.ReadLine();
if (shape.Equals("triangle", StringComparison.OrdinalIgnoreCase))
{
CalculateTriangle();
}
else if (shape.Equals("rectangle", StringComparison.OrdinalIgnoreCase))
{
CalculateRectangle();
}
else
{
Console.WriteLine("无效的形状选择。");
}
}
static void CalculateTriangle()
{
Console.WriteLine("请输入三角形的三个边长:");
double side1, side2, side3;
if (double.TryParse(Console.ReadLine(), out side1) &&
double.TryParse(Console.ReadLine(), out side2) &&
double.TryParse(Console.ReadLine(), out side3))
{
double perimeter = side1 + side2 + side3;
double s = perimeter / 2;
double area = Math.Sqrt(s * (s - side1) * (s - side2) * (s - side3));
Console.WriteLine("三角形的周长为: " + perimeter);
Console.WriteLine("三角形的面积为: " + area);
}
else
{
Console.WriteLine("输入无效的边长。");
}
}
static void CalculateRectangle()
{
Console.WriteLine("请输入长方形的长度和宽度:");
double length, width;
if (double.TryParse(Console.ReadLine(), out length) &&
double.TryParse(Console.ReadLine(), out width))
{
double perimeter = 2 * (length + width);
double area = length * width;
Console.WriteLine("长方形的周长为: " + perimeter);
Console.WriteLine("长方形的面积为: " + area);
}
else
{
Console.WriteLine("输入无效的尺寸。");
}
}
}
- 编写一个控制台应用程序,可根据输入的月份判断所在季节。
using System;
class Program
{
static void Main()
{
Console.WriteLine("请输入月份(1-12):");
if (int.TryParse(Console.ReadLine(), out int month))
{
string season = GetSeason(month);
Console.WriteLine($"该月份属于{season}季节。");
}
else
{
Console.WriteLine("无效的输入,请输入一个有效的月份。");
}
}
static string GetSeason(int month)
{
switch (month)
{
case 12:
case 1:
case 2:
return "冬";
case 3:
case 4:
case 5:
return "春";
case 6:
case 7:
case 8:
return "夏";
case 9:
case 10:
case 11:
return "秋";
default:
return "无效月份";
}
}
}
3. 编写程序,用 while 循环语句实现下列功能:有一篮鸡蛋,不止一个,有人两个两
个数,多余一个,三个三个数,多余一个,再四个四个地数,也多余一个,请问这篮鸡蛋至
少有多少个。
using System;
class Program
{
static void Main()
{
int eggs = 1; // 从1个鸡蛋开始尝试
while (true)
{
if (eggs % 2 == 1 && eggs % 3 == 1 && eggs % 4 == 1)
{
Console.WriteLine($"鸡蛋篮子至少有{eggs}个鸡蛋。");
break;
}
eggs++;
}
}
}
- 编写程序,计算数组中奇数之和和偶数之和。
using System;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int oddSum = 0;
int evenSum = 0;
foreach (int number in numbers)
{
if (number % 2 == 0)
evenSum += number;
else
oddSum += number;
}
Console.WriteLine($"奇数之和为: {oddSum}");
Console.WriteLine($"偶数之和为: {evenSum}");
}
}
5. 编写程序,找一找一个二维数组中的鞍点(即该位置上的元素值在行中最大,在该
列上最小。有可能数组没有鞍点)。要求:
u u 二维数组的大小、数组元素的值在运行时输入;
u u 程序有友好的提示信息。
using System;
class Program
{
static void Main()
{
Console.WriteLine("请输入二维数组的行数和列数:");
if (int.TryParse(Console.ReadLine(), out int rows) && int.TryParse(Console.ReadLine(), out int columns))
{
int[,] matrix = new int[rows, columns];
Console.WriteLine("请输入二维数组的元素值:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if (int.TryParse(Console.ReadLine(), out matrix[i, j]) == false)
{
Console.WriteLine("输入无效的元素值。");
return;
}
}
}
FindSaddlePoint(matrix);
}
else
{
Console.WriteLine("输入无效的行数或列数。");
}
}
static void FindSaddlePoint(int[,] matrix)
{
int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
int value = matrix[i, j];
bool isSaddlePoint = true;
// Check the row for the maximum value
for (int k = 0; k < columns; k++)
{
if (matrix[i, k] > value)
{
isSaddlePoint = false;
break;
}
}
// Check the column for the minimum value
for (int k = 0; k < rows; k++)
{
if (matrix[k, j] < value)
{
isSaddlePoint = false;
break;
}
}
// Print the saddle point if found
if (isSaddlePoint)
{
Console.WriteLine($"鞍点位置:({i + 1}, {j + 1}),值为:{value}");
return;
}
}
}
Console.WriteLine("数组中无鞍点。");
}
}
四、实验总结
通过完成这个实验,你应该对Visual Studio环境的使用、控制台程序的编写、C#程序设计语言的基本语法以及控制语句和数组的使用有了深入的了解。这些技术和概念是你进一步学习和实践C#编程的基础。继续进行更多的练习和实验,不断提升自己的编程技能。
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号