首页 > 代码库 > 初识Java-分数录入系统

初识Java-分数录入系统

package classTest;

import java.util.Scanner;

public class scoreArrangement {
 /**
  * 选择界面(main)
  */
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  String[] name = new String[2];
  String[] password = new String[2];
  int i = 0;// main函数和register函数里面,数组的下标
  while (true) {
   System.out.println("选择菜单");
   System.out.print("A、注册      B、登陆      C、退出");
   String choice = sc.next();
   switch (choice) {
   case "a":
   case "A":
    if (name[1] == null) {
     System.out.println(name[0] + "   " + password[0]);
     System.out.println(name[1] + "   " + password[1]);
     i = register(name, password, i);
     break;
    } else {
     System.out.println("已注册满!");
     break;
    }
   case "b":
   case "B":
    dengLu(name, password);
    break;
   case "c":
   case "C":
    System.out.println("退出成功");
    System.exit(-1);
    break;
   default:
    System.out.println("无效输入,重新选择!");
    break;
   }
  }
 }

 /**
  * 注册
  */
 public static int register(String[] a, String[] b, int x) {
  Scanner sc = new Scanner(System.in);
  System.out.print("请输入用户名:");
  a[x] = sc.next();
  System.out.println("请输入密码:");
  b[x] = sc.next();
  System.out.println("注册成功,用户名为:" + a[x] + "     密码为:" + b[x]);
  x++;
  return x;
 }

 /**
  * 登陆
  */
 public static void dengLu(String[] a, String[] b) {

  Scanner sc = new Scanner(System.in);
  int times = 0;
  System.out.println(a[0] + "   " + b[0]);
  System.out.println(a[1] + "   " + b[1]);
  while (times < 3) {
   System.out.print("请输入用户名:");
   String name = sc.next();
   System.out.println("请输入密码:");
   String password = sc.next();
   for (int x = 0; x < 2; x++) {
    if (name.equals(a[x]) && password.equals(b[x])) {
     fenShuGuanLi();
     times = 3;
     break;
    } else {
     System.out.println("用户名或者密码不对,重新输入。");
     times++;
     if (times == 3) {
      System.out.println("3次输入错误,退出!");
      // System.exit(-1);如果需要停止运行,加上此行代码。
     }
     break;
    }
   }
  }
 }

 /**
  * 分数管理
  */
 public static void fenShuGuanLi() {
  String[] name = new String[100];
  double[] score = new double[100];
  Scanner sc = new Scanner(System.in);
  int index = 1;
  int number = 0;// 学生姓名数组下标
  while (index == 1) {
   System.out.println("输入选择:");
   System.out.println("1、录入学生成绩。");
   System.out.println("2、修改学生成绩。");
   System.out.println("3、删除学生成绩。");
   System.out.println("4、查询单个学生成绩。");
   System.out.println("5、查询所有学生成绩。");
   System.out.println("6、平均成绩。");
   System.out.println("7、返回上级菜单。");
   int choice = sc.nextInt();
   switch (choice) {
   case 1:
    if(number==99){
     System.out.println("数据已满,不能继续录入。");
     break;
    }
    number = luRuFenShu(name, score, number);
    break;
   case 2:
    xiuGaiFenShu(name, score, number);
    break;
   case 3:
    shanChuFenShu(name,score,number);
    break;
   case 4:
    chaXunFenShu(name, score);
    break;
   case 5:
    chaXunAll(name,score,number);
    break;
   case 6:
    average(name,score,number);
    break;
   case 7:
    index = 0;
    break;
   default:
    System.out.println("无效选择,重新输入。");
    break;
   }
  }
 }

 /**
  * 录入分数
  */
 public static int luRuFenShu(String[] name, double[] score, int number) {
  if (number > 99) {
   System.out.println("存储空间不足,不能录入!返回上级菜单。");
   return number;
  }
  // System.out.println("Welcome lurufenshu!");
  Scanner sc = new Scanner(System.in);
  int index = 0;
  while (index == 0) {
   System.out.println("请输入学生姓名:");
   String userName = sc.next();
   name[number] = userName;
   System.out.println("请输入学生分数:");
   double userScore = sc.nextDouble();
   score[number] = userScore;
   System.out.println("录入信息成功,是否继续录入?Y/N");
   int index1 = 0;
   while (index1 == 0) {
    String choice = sc.next();
    switch (choice) {
    case "y":
    case "Y":
     number++;
     index1 = 1;
     break;
    case "n":
    case "N":
     number++;
     return number;
    default:
     System.out.println("无效指令,请重新输入:");
     break;
    }
   }
  }
  return number;
 }

 /**
  * 修改分数
  */
 public static void xiuGaiFenShu(String[] name, double[] score, int number) {
  System.out.println("请输入学生名字:");
  Scanner sc = new Scanner(System.in);
  if (name[0] != null) {
   String userName = sc.next();
   int i = 0;
   for (i = 0; i < number; i++) {
    if (name[i].equals(userName)) {
     System.out.println("请输入修改后的分数:");
     score[i] = sc.nextDouble();
     System.out.println("修改成功!  " + name[i] + "  " + score[i] + "分");
     break;
    }
   }
   if (i == number) {
    System.out.println("没有该学生信息!");
   }
  } else {
   System.out.println("数据库为空,请先录入数据!");
  }
 }

 /**
  * 删除分数
  */
 public static int shanChuFenShu(String[]name,double[]score,int number) {
  Scanner sc=new Scanner(System.in);
  System.out.println("请输入要删除成绩的学生名字:");
  String userName=sc.next();
  for(int i=0;i<=number;i++){
   if(name[i].equals(userName)&&i==99){
    System.out.println("删除成功!");
    name[i]=null;
    score[i]=0;
   }else if(name[i].equals(userName)){
    System.out.println("删除成功!");
    name[i]=name[i+1];
    name[i+1]=null;
    score[i]=score[i+1];
    score[i]=0;
    number--;
   }
  }
  return number;
 }

 /**
  * 查询单个分数
  */
 public static void chaXunFenShu(String[] name, double[] score) {
  System.out.println("请输入学生名字:");
  Scanner sc = new Scanner(System.in);
  String userName = sc.next();
  if (name[0] != null) {
   int i = 0;
   for (i = 0; i < name.length - 1; i++) {
    if (name[i].equals(userName)) {
     System.out.println("该学生的成绩是:" + score[i] + "分。");
     break;
    }
   }
   if (i == name.length - 1) {
    System.out.println("没有该学生信息!");
   }
  } else {
   System.out.println("数据库为空,请先录入成绩!");
  }
 }

 /**
  * 查询全部分数
  */
 public static void chaXunAll(String[]name,double[]score,int number) {
  if (name[0] != null) {
   for(int i=0;i<number;i++){
    System.out.println(name[i]+"、"+score[i]+"分.");
    }
  } else {
   System.out.println("数据库为空,请先录入成绩!");
  } 
 }

 /**
  * 求平均分
  */
 public static void average(String[]name,double[]score,int number) {
  double averageScore=0;
  double sum=0;
  for(int i=0;i<name.length-1;i++){
   sum+=score[i];
  }
  System.out.println("平均分为:"+sum/(number+1));
 }
}

初识Java-分数录入系统