学生信息的添加与查询

Problem Description
设计一个学生添加和查询的系统,从键盘读入学生的数据,然后再从屏幕显示出来。
Input
第一行有2个整数N和M,其中:N——学生数量,M——学生属性数量;
第二行有M个字符串,表示学生的属性名称,其中第1个属性id表示关键字;其中各字段属性的数据类型是确定的。
接下来有N行M列数据,分别表示学生各种属性的值,关键字相同的记录代表一个学生(后来读入的信息覆盖前面读入数据)
Output
输出所有学生的属性及数据。(每行的列数据之间用‘\t’进行分隔)
Example Input
5 4
id name birthday score
0001 Mike 1990-05-20 98.5
0002 John 1992-05-20 67
0003 Hill 1994-05-02 36.5
0004 Christ 1996-05-20 86.5
0001 Jack 1998-05-20 96
Example Output
id:0001	name:Jack	birthday:1998_5_20	score:96.0
id:0002	name:John	birthday:1992_5_20	score:67.0
id:0003	name:Hill	birthday:1994_5_2	score:36.5
id:0004	name:Christ	birthday:1996_5_20	score:86.5
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

class student {
	public String id;
	public String name;
	public String birthday;
	public double score;

	public student(String id, String name, String birthday, double score) {
		super();
		this.id = id;
		this.name = name;
		this.birthday = birthday;
		this.score = score;
	}

	public student() {

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}
	public  static String datechange(String birthday)            //静态可以直接用
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1=new SimpleDateFormat("yyyy_M_d");
		String date=null;                                         //必须有初始值
		try {
			date = sdf1.format(sdf.parse(birthday));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return date;
	}

	@Override
	public String toString() {
		return "id:" + id + "\tname:" + name + "\tbirthday:"
				+datechange(birthday) + "\tscore:" + score ;
	}
	
}

public class Main {

	public static void main(String[] args) throws ParseException {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		int m = cin.nextInt();
		cin.nextLine();
		cin.nextLine();
		Map <String,student> map= new HashMap<String,student>();
		for (int i = 0; i < n; i++) {
			 student stu=new student(cin.next(),cin.next(),cin.next(),cin.nextDouble());
			map.put(stu.getId(),stu);     
		}
		List<String> list=new ArrayList<String>(map.keySet());    //keyset是指strdent.id
         TreeSet<String> set=new TreeSet<String>(list);           //TreeSet可直接排序
         for(String id:set)                                       //按set输出
         {
        	 System.out.println(map.get(id));
         }
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
了解,学生信息查询是一个常见的功能,可以通过以下步骤来实现: 1. 设计数据库 首先需要设计一个合适的数据库来存储学生信息,可以考虑以下: - 学生students):包含学生的基本信息,如学号、姓名、性别、出生日期、班级等字段; - 班级(classes):包含班级的基本信息,如班级编号、班级名称等字段。 2. 建立连接 在 C# 中,可以使用 ADO.NET 来连接数据库。可以参考以下代码: ```csharp using System.Data.SqlClient; // 连接字符串 string connStr = "Data Source=server;Initial Catalog=database;User ID=user;Password=password;"; // 连接数据库 using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); // 数据库操作... } ``` 3. 实现查询功能 在界面中,可以添加控件,如文本框、按钮、列框等,用于输入查询条件和显示查询结果。可以参考以下代码: ```csharp // 查询按钮点击事件 private void btnQuery_Click(object sender, EventArgs e) { // 获取查询条件 string studentID = txtStudentID.Text; // 查询语句 string sql = "SELECT * FROM students WHERE student_id=@studentID"; // 参数 SqlParameter[] parameters = { new SqlParameter("@studentID", studentID) }; // 连接数据库 using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); // 执行查询 SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddRange(parameters); SqlDataReader reader = cmd.ExecuteReader(); // 显示查询结果 lstResult.Items.Clear(); while (reader.Read()) { string name = reader.GetString(reader.GetOrdinal("name")); int age = reader.GetInt32(reader.GetOrdinal("age")); string gender = reader.GetString(reader.GetOrdinal("gender")); lstResult.Items.Add($"{name}\t{age}\t{gender}"); } reader.Close(); } } ``` 以上就是学生信息查询实现思路,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值