Java:学生信息的录入,各种排序,对文件的操作

要求:

     1.请输入5个学生

     2.按总分排序

     3.按数学成绩排序

     4.按语文成绩排序

     5.按计算机成绩排序

     6.单得成绩均大于85分的学生

     7.将结果输出至文本文件

     0.退出系统

package test_java;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

import java.io.*;

@SuppressWarnings("serial")

class Grade implements Serializable

{

private int ID;

private String Name;

private int Math;

private int Chinese;

private int Computer;

private int Total;

Grade() //对私有变量进行初始化

{

ID=0;

Name="";

Math=0;Chinese=0;Computer=0;

Total=0;

}

void setGrade(int id,String name,int math,int chinese,int computer)

{

ID=id;

Name=name;

Math=math;

Chinese=chinese;

Computer=computer;

Total=Math+Chinese+Computer;

}

int getID()

{

return ID;

}

String getName()

{

return Name;

}

int getMath()

{

return Math;

}

int getChinese()

{

return Chinese;

}

int getComputer()

{

return Computer;

}

int getTotal()

{

return Total;

}

void show()

{

System.out.print(ID);System.out.print('\t');

System.out.print(Name);System.out.print('\t');

System.out.print(Math);System.out.print('\t');

System.out.print(Chinese);System.out.print('\t');

System.out.print(Computer);System.out.print('\t');

System.out.print(Total);System.out.println('\t');

}

}

interface Ifun

{

abstract int quickpatition(Grade r[],int low,int high);

abstract void chinese_sort(Grade stu[],int low,int high);

abstract void bubble_sort(Grade stu[],int n);

}

abstract class chinesesort implements Ifun

{

int quickpartition(Grade r[],int low,int high)

{

int i,j;

if(low<high)

{

i=low;

j=high;

Grade w;

w=r[low];

while(i<j)

{

while((r[j].getChinese()>w.getChinese()) && (i<j))

j--;

if(i<j)

r[i++]=r[j];

while((r[i].getChinese()<=w.getChinese()) && (i<j))

i++;

if(i<j)

r[j--]=r[i];

}

r[i]=w;

}

return low;

}

public void chinese_sort(Grade stu[],int low,int high)

{

if(low<high)

{

int temp=quickpartition(stu,low,high);

chinese_sort(stu,low,temp-1);

chinese_sort(stu,temp+1,high);

}

}

}

//冒泡排序类

class bubble

{

}

public class java1 

{

//退出菜单

static void _exit()

{

System.out.println("************************************************");

System.out.println("");

System.out.println("     欢迎使用本系统");

System.out.println("");

System.out.println("         再见!");

System.out.println("");

System.out.println("************************************************");

}

//表头信息

static void table()

{

System.out.print("学号");System.out.print('\t');

System.out.print("姓名");System.out.print('\t');

System.out.print("数学");System.out.print('\t');

System.out.print("语文");System.out.print('\t');

System.out.print("计算机");System.out.print('\t');

System.out.print("总分");System.out.println('\t');

}

//主菜单

static void menu()

{

System.out.println("************************************************");

System.out.println("     1.请输入5个学生");

System.out.println("     2.按总分排序");

System.out.println("     3.按数学成绩排序");

System.out.println("     4.按语文成绩排序");

System.out.println("     5.按计算机成绩排序");

System.out.println("     6.单得成绩均大于85分的学生");

System.out.println("     7.将结果输出至文本文件");

System.out.println("     0.退出系统");

System.out.println("************************************************");

}

//按总成绩冒泡排序

static void bubble_sort(Grade stu[],int n)

{

int i,j;

for(i=0;i<n;i++)

{

for(j=0;j<n-i-1;j++)

{

if(stu[j].getTotal()<stu[j+1].getTotal())

{

Grade temp;

temp=stu[j];

stu[j]=stu[j+1];

stu[j+1]=temp;

}

}

}

}

//按语文成绩--快速排序

static int quickpartition(Grade r[],int low,int high)

{

int i,j;

if(low<high)

{

i=low;

j=high;

Grade w;

w=r[low];

while(i<j)

{

while((r[j].getChinese()>w.getChinese()) && (i<j))

j--;

if(i<j)

r[i++]=r[j];

while((r[i].getChinese()<=w.getChinese()) && (i<j))

i++;

if(i<j)

r[j--]=r[i];

}

r[i]=w;

}

return low;

}

static void chinese_sort(Grade stu[],int low,int high)

{

if(low<high)

{

int temp=quickpartition(stu,low,high);

chinese_sort(stu,low,temp-1);

chinese_sort(stu,temp+1,high);

}

}

//按数学成绩--直接选择排序

static void math_sort(Grade stu[],int n)

{

System.out.println("按数学成绩由小到大排序");

int min,j;

Grade temp;

for(int i=0;i<n-1;i++)

{

min=i;

for(j=i;j<n;j++)

{

if(stu[min].getMath()>stu[j].getMath())

{

min=j;

}

}

if(min!=j)

{

temp=stu[i];

stu[i]=stu[min];

stu[min]=temp;

}

}

}

//按计算机成绩--直接插入排序

static void computer_sort(Grade stu[],int n)

{

System.out.println("按计算机成绩由小到大排序");

int i,j;

Grade temp;

for(i=1;i<n;i++)

{

temp=stu[i];

j=i-1;

while(j>0 && temp.getComputer() < stu[j].getComputer())

{

stu[j+1]=stu[j];

j--;

}

stu[j+1]=temp;

}

}

public static void main(String[] args) throws Exception

{

int num;

int i;

final int N=5;

int id;

String name;

int math;

int chinese;

int computer;

Grade []stu=new Grade[N];

//ArrayList <Grade> student=new ArrayList<Grade>();

menu();

Scanner reader=new Scanner(System.in);

while(true)

{

System.out.print("请选择:");

num=reader.nextInt();

switch(num)

{

case 1:

System.out.println("请输入5个学生的情况:");

//输入5个学生的情况并通过setGrade函数初始化私有变量

for(i=0;i<stu.length;i++)

{

  //System.out.println("请输入学生的学号:");

  stu[i]=new Grade();

  id = reader.nextInt();

  //System.out.println("请输入学生的姓名:");

  name = reader.next();

  //System.out.println("请输入数学成绩:");

  math = reader.nextInt();

  //System.out.println("请输入语文成绩:");

  chinese = reader.nextInt();

  //System.out.println("请输入计算机成绩:");

  computer = reader.nextInt();

  stu[i].setGrade(id,name,math,chinese,computer);

}

table();//获得表头信息

//按输入的顺序显示5个学生的信息

for(i=0;i<stu.length;i++)

{

stu[i].show();

}

break;

//break;

case 2:

System.out.println("按总分冒泡排序");

bubble_sort(stu,N);

table();

for(i=0;i<stu.length;i++)

{

stu[i].show();

}

break;

case 3:

System.out.println("按语文成绩快速排序:");

chinese_sort(stu,0,N-1);

table();

for(i=0;i<N;i++)

{

stu[i].show();

}

break;

case 4:

System.out.println("按数学成绩直接选择排序:");

math_sort(stu,N);

table();

for(i=0;i<stu.length;i++)

{

stu[i].show();

}

break;

case 5:

System.out.println("按计算机成绩直接插入排序:");

computer_sort(stu,N);

table();

for(i=0;i<N;i++)

{

stu[i].show();

}

break;

case 6:

System.out.println("单科成绩大于85分以上的学生:");

for(i=0;i<N;i++)

{

if(stu[i].getMath()>85 &&

stu[i].getChinese()>85 &&

stu[i].getComputer()>85)

stu[i].show();

}

break;

case 7:

System.out.println("将文件结果输出至文本文件");

for(i=0;i<N;i++)

{

System.out.println("stu的对象id:"+stu[i].getID());

System.out.println("stu的对象id:"+stu[i].getName());

System.out.println("stu的对象id:"+stu[i].getMath());

}

FileOutputStream fos=new FileOutputStream("c:\\aa\\file1.txt");

ObjectOutputStream oos=new ObjectOutputStream(fos);

oos.writeObject(stu);

/*

try

{

ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("c:/file1.txt"));

os.writeObject(stu);

os.flush();

os.close();

}catch(FileNotFoundException e)

{

e.printStackTrace();

}catch(IOException e)

{

e.printStackTrace();

}

*/

//Random rand=new Random();

/*

FileOutputStream out=new FileOutputStream("/Users/sx_dqbsina.com/file2.txt");

ObjectOutputStream oos=new ObjectOutputStream(out);

oos.writeObject(stu);

oos.flush();

oos.close();

//byte b[]=stu

FileReader reader=new FileReader(stu);

FileWriter writer=new FileWriter("/Users/sx_dqbsina.com/file2.txt");

//writer.write(stu);

//BufferedWriter output;

//try {

output = new BufferedWriter(new FileWriter("/Users/sx_dqbsina.com/file2.txt"));

//} catch (IOException e) {

// TODO Auto-generated catch block

// e.printStackTrace();

}

BufferedWriter bw=new BufferedWriter(writer);

int []num1=new int[15];

for(i=0;i<N;i++)

{

num1[i]=20+rand.nextInt(9979);

output.write(num1[i]+"");

output.newLine();

}

//output.close();

*/

break;

case 0:

_exit();

System.exit(1);

//System.out.println("0");

break;

default:

System.out.println("没有此项目!");

//break;

}

}

//System.out.println(z);

}

}

原文地址:https://www.cnblogs.com/duanqibo/p/11102955.html