用C#写上面那段代码
时间: 2023-08-10 20:19:50 浏览: 70
好的,以下是C#版本的代码:
```csharp
using System;
public class Person {
public string name;
public string id;
public int age;
public char gender;
}
public class Student : Person {
public string major;
}
public class Teacher : Person {
public string title;
}
class Program {
static void Main(string[] args) {
Student[] students = new Student[3];
Teacher teacher = new Teacher();
// 创建学生信息库
for (int i = 0; i < 3; i++) {
Console.Write($"请输入第{i+1}名学生的姓名:");
students[i] = new Student();
students[i].name = Console.ReadLine();
Console.Write($"请输入第{i+1}名学生的学号:");
students[i].id = Console.ReadLine();
Console.Write($"请输入第{i+1}名学生的年龄:");
students[i].age = int.Parse(Console.ReadLine());
Console.Write($"请输入第{i+1}名学生的性别(m/f):");
students[i].gender = char.Parse(Console.ReadLine());
Console.Write($"请输入第{i+1}名学生的专业:");
students[i].major = Console.ReadLine();
}
// 创建教师信息
Console.Write("请输入教师的姓名:");
teacher.name = Console.ReadLine();
Console.Write("请输入教师的工号:");
teacher.id = Console.ReadLine();
Console.Write("请输入教师的年龄:");
teacher.age = int.Parse(Console.ReadLine());
Console.Write("请输入教师的性别(m/f):");
teacher.gender = char.Parse(Console.ReadLine());
Console.Write("请输入教师的职称:");
teacher.title = Console.ReadLine();
// 输出学生信息
Console.WriteLine("\n学生信息:");
foreach (Student student in students) {
Console.WriteLine($"姓名:{student.name}");
Console.WriteLine($"学号:{student.id}");
Console.WriteLine($"年龄:{student.age}");
Console.WriteLine($"性别:{student.gender}");
Console.WriteLine($"专业:{student.major}");
}
// 输出教师信息
Console.WriteLine("\n教师信息:");
Console.WriteLine($"姓名:{teacher.name}");
Console.WriteLine($"工号:{teacher.id}");
Console.WriteLine($"年龄:{teacher.age}");
Console.WriteLine($"性别:{teacher.gender}");
Console.WriteLine($"职称:{teacher.title}");
}
}
```
输出结果与C版本一致,就不再重复了。
阅读全文