初識Java-分數錄入系統

來源:互聯網
上載者:User

標籤:分數   輸入密碼   switch   average   print   sum   use   index   register   

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-分數錄入系統

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.