1、编写程序求将一个5行5列的二维数组对角线赋值为0,其他值赋值为1。 2、编写一个学生类,有学号、姓名、性别、年龄属性。提供构造函数、get set操作。提供方法。输入10个学生、按照学生的年龄从小到大输出学生信息。
(1)
int[,] arr = new int[5,5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (i == j) arr[i,j] = 0; else arr[i,j] = 1;
}
}
另一个问题采纳后写给你。
(2)
class student
{
public string 学号 { get; set; }
public string 姓名 { get; set; }
public bool 性别 { get; set; }
public int 年龄 { get; set; }
public student(string 学号, string 姓名, bool 性别, int 年龄)
{
this.学号 = 学号;
this.姓名 = 姓名;
this.性别 = 性别;
this.年龄 = 年龄;
}
}
void main()
{
student[] arr = new student[10];
for (int i = 0; i < 10; i++)
{
string 学号 = Console.ReadLine();
string 姓名 = Console.ReadLine();
bool 性别 = Console.ReadLine() == "男";
int 年龄 = int.Parse(Console.ReadLine());
arr[i] = new student(学号, 姓名, 性别, 年龄);
}
foreach (student s in arr.OrderBy(x = x.年龄))
{
Console.WriteLine("{0} {1} {2} {3}", s.学号, s.姓名, s.性别, s.年龄);
}
}
完整的程序
using System;
using System.Linq;
class student
{
public string 学号 { get; set; }
public string 姓名 { get; set; }
public bool 性别 { get; set; }
public int 年龄 { get; set; }
public student(string 学号, string 姓名, bool 性别, int 年龄)
{
this.学号 = 学号;
this.姓名 = 姓名;
this.性别 = 性别;
this.年龄 = 年龄;
}
}
public class Test
{
public static void Main()
{
student[] arr = new student[10];
for (int i = 0; i < 10; i++)
{
string 学号 = Console.ReadLine();
string 姓名 = Console.ReadLine();
bool 性别 = Console.ReadLine() == "男";
int 年龄 = int.Parse(Console.ReadLine());
arr[i] = new student(学号, 姓名, 性别, 年龄);
}
foreach (student s in arr.OrderBy(x => x.年龄))
{
Console.WriteLine("{0} {1} {2} {3}", s.学号, s.姓名, s.性别, s.年龄);
}
}
}
输入
1
a1
男
20
2
a2
男
22
3
a3
女
24
4
a4
男
19
5
a5
女
22
6
a6
男
26
7
a7
男
25
8
a8
女
30
9
a9
男
24
10
a10
男
20
输出
4 a4 True 19
1 a1 True 20
10 a10 True 20
2 a2 True 22
5 a5 False 22
3 a3 False 24
9 a9 True 24
7 a7 True 25
6 a6 True 26
8 a8 False 30