0906小练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了0906小练习相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace 练习0906
{
class Program
{
struct student
{
public int num;
public string code;
public string name;
public decimal score;
}
public string pdc(string a)
{
if (a != null)
{ return a; }
else
{ return "f";}
}
public string pdn(string a)
{
if (a != null)
{ return a; }
else
{ return "f"; }
}
public decimal pds(decimal a)
{
if (a>=0&&a<=100)
{ return a; }
else
{ return -1; }
}
static void Main(string[] args)
{
Program hanshu=new Program();
ArrayList st = new ArrayList();
student student =new student();
Console.Write("请输入班级人数:");
int n = int.Parse(Console.ReadLine());
int num = 1;
for (int i = 0; i < n; i++)
{
student.num = num;
Console.Write("请输入第" + (i + 1) + "个学生的学号:");
student.code=Console.ReadLine();
for (; ; )
{
if (hanshu.pdc(student.code) == "f")
{
Console.Write("序号不能为空,请重新输入第" + (i + 1) + "个学生的学号:");
student.code = Console.ReadLine();
}
else
{
break;
}
}
Console.Write("请输入第" + (i + 1) + "个学生的名字:");
student.name=Console.ReadLine();
for (; ; )
{
if (hanshu.pdn(student.name) == "f")
{
Console.Write("姓名不能为空,请重新输入第" + (i + 1) + "个学生的名字:");
student.name = Console.ReadLine();
}
else
{
break;
}
}
Console.Write("请输入第" + (i + 1) + "个学生的成绩:");
try
{
student.score = decimal.Parse(Console.ReadLine());
for (; ; )
{
if (hanshu.pds(student.score) == -1)
{
Console.Write("成绩输入有误,请重新输入第" + (i + 1) + "个学生的成绩:");
student.score = decimal.Parse(Console.ReadLine());
}
else
{
break;
}
}
}
catch { Console.WriteLine("成绩输入必须为数字!"); }
num++;
st.Add(student);
Console.WriteLine("-------------------------------");
}
Console.Write("输入完毕请按回车键打印学生信息:");
Console.ReadLine();
for (int i = 0; i < n-1; i++)
{
for (int j = i+1; j < n; j++)
{
student a = (student)(st[i]);
student b = (student)(st[j]);
if (a.score< b.score)
{
object zhong = st[i];
st[i] = st[j];
st[j] = zhong;
}
}
}
Console.WriteLine("==============学生成绩展示=================");
Console.WriteLine("序号" + "\t"+"\t"+"学号"+"\t"+"\t"+"姓名" + "\t"+"\t"+"成绩");
for (int i = 0; i < n;i++)
{
student a = (student)(st[i]);
Console.Write(a.num);Console.Write("\t");Console.Write("\t");
Console.Write(a.code);Console.Write("\t");Console.Write("\t");
Console.Write(a.name);Console.Write("\t");Console.Write("\t");
Console.Write(a.score);Console.Write("\t");Console.Write("\t");
Console.WriteLine();
}
Console.ReadLine();
}
}
}
以上是关于0906小练习的主要内容,如果未能解决你的问题,请参考以下文章