using System;
using System.IO;
namespace Experiment1
{
class Program
{
static void Main(string[] args)
{
CircleArea();
VowelRecognition();
Factorial();
Survey();
}
// 1-1 计算圆面积
static void CircleArea()
{
double radius, area;
const double Pi = 3.14;
Console.WriteLine("请输入圆的半径:");
radius = Convert.ToDouble(Console.ReadLine());
area = Pi * radius * radius;
Console.WriteLine("圆的面积为:" + area);
}
// 1-2 识别元音字母
static void VowelRecognition()
{
char c;
Console.WriteLine("请输入一个字母:");
c = Convert.ToChar(Console.ReadLine().ToLower());
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
Console.WriteLine(c + " 是元音字母。");
}
else
{
Console.WriteLine(c + " 不是元音字母。");
}
}
// 1-3 阶乘
static void Factorial()
{
int n, factorial = 1;
Console.WriteLine("请输入一个正整数:");
n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
factorial *= i;
}
Console.WriteLine(n + " 的阶乘为:" + factorial);
}
// 1-4 调查表
static void Survey()
{
string name, gender, age;
int count = 0;
// 创建或打开文件,准备写入数据
StreamWriter sw = new StreamWriter("survey.txt");
while (true)
{
Console.Write("请输入被调查者姓名:");
name = Console.ReadLine();
Console.Write("请输入被调查者性别:");
gender = Console.ReadLine();
Console.Write("请输入被调查者年龄:");
age = Console.ReadLine();
Console.WriteLine(name + " " + gender + " " + age + " 岁");
// 将数据写入文件
sw.WriteLine(name + "," + gender + "," + age);
count++;
Console.Write("是否继续输入下一个被调查者信息?(y/n):");
string answer = Console.ReadLine();
if (answer.ToLower() == "n")
{
break;
}
}
// 关闭文件
sw.Close();
Console.WriteLine("共有 " + count + " 个被调查者参与了调查。");
Console.WriteLine("调查结果已保存到 survey.txt 文件中。");
}
}
}
流程控制C#
最新推荐文章于 2024-08-03 23:17:32 发布