码迷,mamicode.com
首页 > 编程语言 > 详细

java用数组实现简单的学生管理系统

时间:2015-07-18 00:15:49      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

用数组存放对象,然后遍历数组,加上一些数组的操作,很简单,纯属记录

import java.util.Scanner;


public class StudentManagerTwice {
    public static void main(String[] args) {
        Student1 stu = new Student1("aaa",12,1201);
        Student1 stu2 = new Student1("bbb",34,1202);
        Student1 stu3 = new Student1("ccc",14,1203);
        Student1 stu4 = new Student1("ddd",16,1204);
        Student1 stu5 = new Student1("eee",11,1204);
        
        Student1[] stus = new Student1[5];
        
        stus[0] = stu;
        stus[1] = stu2;
        stus[2] = stu3;
        stus[3] = stu4;
        stus[4] = stu5;
        
        String answer = "";
        Scanner input = new Scanner(System.in);
        System.out.println("-----welcomecome to the students manager system-----");
        do {
            
            System.out.println("     1.look all         2.query stuNum    ");
            System.out.println("     3.exit             4.without    ");
            int choice = input.nextInt();
            switch (choice) {
            case 1:
                for (int i = 0; i < stus.length; i++) {
                    System.out.println(stus[i]);
                }
                break;
            case 2:
                System.out.println("please input a stuNum: ");
                int num = input.nextInt();
                for (int i = 0; i < stus.length; i++) {
                    Student1 s = stus[i];
                    if(num == s.getStuNum()){
                        System.out.println(s);
                        break;  //这里用break提前结束了循环,提高了效率,但是
                    }
                }
                break;
            case 3:
                System.exit(0);
                break;
            default:
                break;
            }
            System.out.println("continue?  <y/n>");
            answer = input.next();
        } while (answer.equals("y"));
            
    
    }
    
}



class Student1{
    private String name;
    private int age;
    private int stuNum;
    public Student1() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student1(String name, int age, int stuNum) {
        super();
        this.name = name;
        this.age = age;
        this.stuNum = stuNum;
    }
    @Override
    public String toString() {
        return "Studetn [name=" + name + ", age=" + age + ", stuNum=" + stuNum
                + "]";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getStuNum() {
        return stuNum;
    }
    public void setStuNum(int stuNum) {
        this.stuNum = stuNum;
    }
    
    
}

java用数组实现简单的学生管理系统

标签:

原文地址:http://www.cnblogs.com/ccharp/p/4655848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!