没正式学过java,但是系统学过C++后,初略的看了下java的基本语法,于是我就尝试着用java来写个简单的学生管理系统,功能不齐全,以后有空再补充吧。
写的时候定义了不同的包名字,如jeaven1 , jeaven2 , jeaven3
先上Student类:
package jeaven1;
import java.io.*; public class Student
{
private String name;
private String id;
private String sex;
private int age;
private double grade;
public Student next; //public给外部直接访问next的权限 public Student(String _name,String _id,String _sex,int _age,double _grade)
{
this.name = _name;
this.id = _id;
this.sex = _sex;
this.age = _age;
this.grade = _grade;
this.next = null;
} public Student()
{
this.name = "null";
this.id = "null";
this.sex = "null";
this.age = 0;
this.grade = 0;
this.next = null;
} public void show()
{
System.out.println("名字:"+name+" "+"学号:"+id+" "+"性别:"+sex+" "+"年龄:"+age+" "+"成绩:"+grade);
}
}
再上stulist类
package jeaven2;
import java.io.*;
import jeaven1.Student;
import java.util.Scanner; public class stulist
{
private Student head; //头节点
private int num; //存储学生信息个数
public stulist()
{
head = new Student();
num = 0;
} public void Insert()
{
String _name,_id,_sex;
int _age;
double _grade;
Scanner in = new Scanner(System.in);
System.out.printf("请输入学生姓名: ");
_name = in.nextLine();
System.out.printf("请输入学生学号: ");
_id = in.nextLine();
System.out.printf("请输入学生性别: ");
_sex = in.nextLine();
System.out.printf("请输入学生年龄: ");
_age = in.nextInt();
System.out.printf("请输入学生成绩: ");
_grade = in.nextDouble();
System.out.printf("\n");
Student newstu = new Student(_name,_id,_sex,_age,_grade);
Student p = head;
while(p.next != null)
{
p = p.next;
}
p.next = newstu;
newstu.next = null;
num ++;
} public void display()
{
Student p = head.next;
while(p != null)
{
p.show();
p = p.next;
}
if(num == 0)
System.out.println("此时没有任何学生的信息...");
}
}
再上main类
package jeaven3;
import jeaven1.Student;
import jeaven2.stulist; public class main
{
public static void main(String[] args)
{
stulist list = new stulist();
list.Insert();
list.Insert();
list.display();
}
}
在windows下cmd中编译:
运行结果:
如有不对,欢迎批评指针~