package com.myhomework;
/*作者:陈天祥
时间:2016.10.12
功能:成绩管理系统(增删查改)*/
import java.util.Scanner;
public class Scoremanage {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[][] a = { { "张三", "李四", "王五" } };
int[][] b = { { 123, 456, 789 } };
transfer(a, b);
}
// 管理功能
public static void transfer(String[][] a, int[][] b) {
int select = manu();
switch (select) {
case 1:
newAdd(a, b);
transfer(a, b);
break;
case 2:
delete(a, b);
transfer(a, b);
break;
case 3:
lookup(a, b);
transfer(a, b);
break;
case 4:
change(a, b);
transfer(a, b);
break;
case 5:
break;
case 6:
System.out.println("退出系统");
return;
default:
System.out.println("no exist this function!program ending!");
break;
}
transfer(a, b);
}
// 选择菜单方法
public static int manu() {
System.out
.println("*************congratulation enter manu function***************");
System.out
.println("1:newAdd\n2:delete\n3:lookup\n4:revise\n5:lookup average score\n6:quit\nplease enter one manu");
Scanner s1 = new Scanner(System.in);
int select = s1.nextInt();
while (select < 1 || select > 6) {
System.out.println("不存在该选择,请重新输入");
select = s1.nextInt();
}
return select;
}
// 增加学生及成绩方法
public static void newAdd(String[][] a, int[][] b) {
System.out
.println("**********congratulation enter newAdd function***********");
Scanner s1 = new Scanner(System.in);
System.out.println("please increase a student's name");
String name = s1.nextLine();
for (int i = 0; i < a[0].length; i++) {
while (a[0][i].equals(name)) {
System.out.println("您输入的姓名已存在,请重新输入:");
name = s1.nextLine();
i = 0;
}
}
System.out.println("please enter the student's score");
int score = s1.nextInt();
while (score < 0 || score > 100) {
System.out.println("你输入的成绩超出正常范围,请重新输入:");
score = s1.nextInt();
}
String[] newa = a[0];
int[] newb = b[0];
a[0] = new String[newa.length + 1];
b[0] = new int[newb.length + 1];
for (int j = 0; j < a[0].length; j++) {
if (j == a[0].length - 1) {
a[0][j] = name;
b[0][j] = score;
} else {
a[0][j] = newa[j];
b[0][j] = newb[j];
}
}
System.out.println("增加成功!");
}
// 删除学生及成绩方法
public static void delete(String a[][], int[][] b) {
System.out
.println("*************congratulation enter delete function***************");
Scanner s1 = new Scanner(System.in);
System.out.println("please enter the student you want to delete");
System.out.println("请输入您要删除的学生名字");
String name = s1.nextLine();
int index = -1;
for (int i = 0; i < a[0].length; i++) {
if (name.equals(a[0][i])) {
index = i;
}
}
if (index == -1) {
System.out.println("您输入的学生不存在:\n1:重新输入\n2退出删除");
int input = s1.nextInt();
switch (input) {
case 1:
delete(a, b);
break;
case 2:
System.out.println("结束删除!");
break;
default:
System.out.println("输入无效,删除结束!");
}
}else{
String[] newa=new String[a[0].length];
int[] newb=new int[a[0].length];
newa=a[0];
newb=b[0];
a[0]=new String[newa.length-1];
b[0]=new int[newb.length-1];
if(index==newa.length){
for(int i=0;i
a[0][i]=newa[i];
b[0][i]=newb[i];
}
}
else{
for(int j=0;j
if(index>j){
a[0][j]=newa[j];
b[0][j]=newb[j];
}
if(index<=j){
a[0][j]=newa[j+1];
b[0][j]=newb[j+1];
}
}
}
System.out.println("删除完成!");
}
}
// 查找方法
public static void lookup(String[][] a, int[][] b) {
System.out
.println("*************congratulation enter lookup function***************");
Scanner s1 = new Scanner(System.in);
System.out.println("请选择查询方式\n1查看单个学生成绩:\n2查看所有学生成绩:");
int select = s1.nextInt();
switch (select) {
case 1:
onelookup(a, b);
break;
case 2:
for (int i = 0; i < a[0].length; i++) {
System.out.println(a[0][i] + "的成绩为:" + b[0][i]);
}
break;
default:
System.out.println("错误,结束查询");
}
}
// 查找单个学生的成绩
public static void onelookup(String[][] a, int[][] b) {
System.out.println("请输入您要查询的学生名字");
Scanner s1 = new Scanner(System.in);
String name = s1.nextLine();
int index = -1;
for (int i = 0; i < a[0].length; i++) {
if (name.equals(a[0][i])) {
index = i;
}
}
if (index == -1) {
System.out.println("您输入的学生不存在:\n1:重新输入\n2退出查询");
int input = s1.nextInt();
switch (input) {
case 1:
onelookup(a, b);
break;
case 2:
System.out.println("结束查询!");
break;
default:
System.out.println("输入无效,查询结束!");
}
} else {
System.out.println(a[0][index] + "的成绩为:" + b[0][index]);
}
}
// 计算下标方法
public static int count(String[][] a, int[][] b, String name) {
for (int i = 0; i < a[0].length; i++) {
if (name.equals(a[0][i])) {
return i;
}
}
return -1;
}
// 修改学生成绩方法
public static void change(String[][] a, int[][] b) {
System.out
.println("*************congratulation enter revise function***************");
System.out.println("请输入您要修改的学生名字");
Scanner s1 = new Scanner(System.in);
String name = s1.nextLine();
int index = -1;
for (int i = 0; i < a[0].length; i++) {
if (name.equals(a[0][i])) {
index = i;
}
}
if (index == -1) {
System.out.println("您输入的学生不存在:\n1:重新输入\n2退出修改");
int input = s1.nextInt();
switch (input) {
case 1:
change(a, b);
break;
case 2:
System.out.println("结束修改!");
break;
default:
System.out.println("输入无效,修改结束!");
}
} else {
System.out.println("请输入您要修改的分数:");
int score = s1.nextInt();
if (score < 0 || score > 100) {
System.out.println("您输入的成绩无效!\n1:重新输入\n2:结束修改");
int input = s1.nextInt();
switch (input) {
case 1:
change(a, b);
break;
case 2:
System.out.println("结束修改!");
break;
default:
System.out.println("输入无效,修改结束!");
}
} else {
b[0][index] = score;
System.out.println("修改成功");
}
}
}
}