日期:2014-05-20  浏览次数:20798 次

对象数组的信息管理系统,求解错误!!!!!!


可以正常输入,,但是每次重新返回菜单查询的时候又是null值了,,
我感觉是 showMemu()方法里面的创建对象,,
如果我执行了添加数据,,按1,又进入了主菜单,,又会重新创建对象数组,所以又是null了,,
请问该怎么该?
让记录会正常保存


Java code
import java.util.*;

public class Main {

        
    void input(Student s[],int n)
    {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("请输入姓名:");
        s[n].name = scan.next();
        
        System.out.println("请输入学号:");
        s[n].num = scan.nextInt();
        
        System.out.println("请输入英语成绩:");
        s[n].english = scan.nextInt();
        
        System.out.println("请输入程序设计程序:");
        s[n].programme = scan.nextInt();
        
        System.out.println("请输入数学成绩:");
        s[n].math = scan.nextInt();
        
        System.out.println("按1返回主菜单,按0退出");
        int b  = scan.nextInt();
        if( b == 1)
            showMemu();
        else
            System.exit(0);
    }
    
    void avgScore(Student s[],int n)
    {
        double average = (s[n].english + s[n].math + s[n].programme) / 3.0;
        System.out.println(average);
        Scanner scan = new Scanner(System.in);
        System.out.println("按1返回主菜单,按0退出");
        int b  = scan.nextInt();
        if( b == 1)
            showMemu();
        else
            System.exit(0);
    }
    
    void searchst(Student s[],int n)
    {
        for(int i = 0;i<s.length;i++)
        {
            if(s[i].num == n)
            {
                System.out.println("姓名:" + s[i].name);
                System.out.println("学号:" + s[i].num);
                System.out.println("英语:" + s[i].english);
                System.out.println("数学:" + s[i].math);
                System.out.println("程序设计:" + s[i].programme);
                break;
            }
        }
        Scanner scan = new Scanner(System.in);
        System.out.println("按1返回主菜单,按0退出");
        int b  = scan.nextInt();
        if( b == 1)
            showMemu();
        else
            System.exit(0);
    }
    
    void sort(Student s[])
    {
        Arrays.sort(s);
        Scanner scan = new Scanner(System.in);
        System.out.println("按1返回主菜单,按0退出");
        int b  = scan.nextInt();
        if( b == 1)
            showMemu();
        else
            System.exit(0);
    }
    
    void display(Student s[])
    {
        for(int i = 0; i < s.length;i++)
        {
            System.out.println("姓名" + s[i].name + "\t" + "学号" + s[i].num + "\t" + "英语成绩:" + s[i].english + "\t" + "程序设计成绩:" + s[i].programme + "\t" + "数学成绩:" + s[i].math);
        }
        Scanner scan = new Scanner(System.in);
        System.out.println("按1返回主菜单,按0退出");
        int b  = scan.nextInt();
        if( b == 1)
            showMemu();
        else
            System.exit(0);
    }
    
    public void showMemu()
    {
        //====================
        //      显示主菜单
        //====================
        System.out.println("==========学籍管理系统==========\n\t" + "1.输入学生信息\n\t"
                + "2.求每门课平均成绩\n\t" + "3.按学号姓名查找学生\n\t" + "4.从高分到低分显示学生信息\n\t"
                + "5.显示学生信息" + "\n\t6.退出");
        
        //创建对象数组
        Student[] info = new Student[5];
        for(int i = 0; i < info.length; i++)
        {
            info[i] = new Student();
        }
        
        //创建记录号
        int n = 0;
        
        //接收一个数作为选择
        Scanner scan = new Scanner(System.in);
        int choose = scan.nextInt();
        switch (choose) {
        case 1:
            input(info, n);
            n++;
        case 2:
            System.out.println("请输入你要求平均分的学生:(0为第一条记录)");
            int num = scan.nextInt();
            avgScore(info, num);
        case 3:
            System.out.println("请输入您查询的学号:");
            int num1 = scan.nextInt();
            searchst(info, num1);
        case 4:
            sort(info);
        case 5:
            display(info);
        case 6:
            System.exit(0);
        }
    }
    
    
    public static void main(String[] args) {
        Main m = new Main();
        Student[] info = new Student[5];
        for(int i = 0; i < info.length; i++)
        {
            info[i] = new Student();
        }
        m.showMemu();
    }
}