java設計簡單學生管理系統(tǒng)
本文實例為大家分享了java學生成績管理系統(tǒng),供大家參考,具體內容如下
要求:
完善Student類,Student類包含學號、姓名、年級、專業(yè)、三門功課的成績(英語、高數(shù)、計算機)的成員變量,完善成績錄入方法、設計按學號查找方法、按姓名查找方法、按單科成績排序的方法。
設計主類,實例化包含5個學生信息的學生數(shù)組,查找某一個學生的信息并打印出來,同時打印這5個學生按某一科成績的按高到低的排序信息(學號、姓名、成績);輸出所有學生的三門單科平均成績。
首先先創(chuàng)建一個student類
使用構造方法來初始化
學號、姓名、年級、專業(yè)、三門功課的成績
先打包
在分類

student類
使用構造方法初始化 get和set方法傳值
package swpu.student;
public class Student {
public String number;
public String name;
public String major;
public int math;
public int computer;
public int english;
public int total;
//對象數(shù)組初始化,使用構造方法
public Student(String newname,String nmajor,String newnumber,int nmath,int ncom,int ne){
number = newnumber;
major =nmajor;
name = newname;
math = nmath;
computer = ncom;
english = ne;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getComputer() {
return computer;
}
public void setComputer(int computer) {
this.computer = computer;
}
}
排序類
rank類
package swpu.student;
public class Rank {
public static void rankscore(Student [] arr,int n){
//數(shù)學
if(n==1) {
for (int i = 0; i < arr.length-1; i++) {
int index = i;
int j;
// 找出最小值得元素下標
for (j = i + 1; j < arr.length; j++) {
if (arr[j].math > arr[index].math) {
index = j;
}
}
int tmp = arr[index].math;
arr[index].math = arr[i].math;
arr[i].math = tmp;
}
}
//英語
if(n==2) {
for (int i = 0; i < arr.length-1; i++) {
int index = i;
int j;
// 找出最小值得元素下標
for (j = i + 1; j < arr.length; j++) {
if (arr[j].english > arr[index].english) {
index = j;
}
}
int tmp = arr[index].english;
arr[index].english = arr[i].english;
arr[i].english = tmp;
}
}
//計算機
if(n==3) {
for (int i = 0; i < arr.length-1; i++) {
int index = i;
int j;
// 找出最小值得元素下標
for (j = i + 1; j < arr.length; j++) {
if (arr[j].computer > arr[index].computer) {
index = j;
}
}
int tmp = arr[index].computer;
arr[index].computer = arr[i].computer;
arr[i].computer = tmp;
}
}
}
}
這里使用了靜態(tài)方法傳入成績的值
查找類
search類
package swpu.student;
public class Search {
//書寫兩種方法(學號,姓名)
public int StuNum(Student arr[] ,String y)//傳入數(shù)組,查找值 ,使用字符串的比較
{
for(int i = 0;i<arr.length;i++)
{
if(arr[i].number.equals(y))
return i;
}
return -1;
}
public int StuNam(Student stu[] ,String x) {
for(int i = 0;i<stu.length;i++)
{
if(stu[i].name.equals(x))
return i;
}
return -1;
}
}
主要類
Instudent類
package swpu.student;
import java.util.Scanner;
public class Instudent {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
Student []stu = new Student[5];
//學生成績初始化
stu[0] = new Student("Jack","軟工 ","20183101",80,90,85);
stu[1] = new Student("Rose","大數(shù)據(jù)","20183102",99,93,90);
stu[2] = new Student("John","網(wǎng)安全","20183103",87,70,74);
stu[3] = new Student("Andi","網(wǎng)工程","20183104",67,66,68);
stu[4] = new Student("Mike","物聯(lián)網(wǎng)","20183105",56,90,55);
//局部變量的初始化
String nu1 = "";
String na1 = "";
String ma1 = "";
int t1=0,t2=0,t3=0;
System.out.println("-------------------學生成績管理系統(tǒng)------------------------");
//輸入學生信息
for(int i=0;i<stu.length;i++) {
System.out.println("請輸入第"+(i+1)+"個學生的姓名,專業(yè),學號,數(shù)學成績,計算機成績,英語成績");
na1 = in.next();//姓名
ma1 = in.next();//專業(yè)
nu1 = in.next();//學號
t1 = in.nextInt();
t2 = in.nextInt();
t3 = in.nextInt();
stu[i].setNumber(nu1);
stu[i].setName(na1);
stu[i].setMajor(ma1);
stu[i].setEnglish(t3);
stu[i].setComputer(t2);
stu[i].setMath(t2);
}
Search search = new Search();
//選擇需要的查找的方法
System.out.println("選擇需要的查找的方法, 1學號,2姓名");
int p = in.nextInt();
if(p==1) {
//使用學號的方法進行查找
System.out.println("輸入您所需要查找的學生學號");
String y = in.next();
int x = search.StuNum(stu,y);
if(x>=0)
System.out.println("學號:"+stu[x].number+" 學生:"+stu[x].name+" 專業(yè):"+stu[x].major+" 數(shù)學:"+stu[x].math+" 計算機:"+stu[x].computer+" 英語:"+stu[x].english);
else
System.out.println("輸入的學生不存在");
}
if(p==2) {
//使用姓名的方法進行查找
System.out.println("輸入您所需要查找的學生姓名");
String thename = in.next();
int w = search.StuNam(stu,thename);
if(w>=0)
System.out.println("學號:"+stu[w].number+" 學生:"+stu[w].name+" 專業(yè):"+stu[w].major+" 數(shù)學:"+stu[w].math+" 計算機:"+stu[w].computer+" 英語:"+stu[w].english);
else
System.out.println("輸入的學生不存在");
}
System.out.println("是否需要對單科成績進行排名 [Y/N] 1 =yes,2=no");
int op = in.nextInt();
if(op==1) {
//單科成績的排序(輸入所需要科目然后直接進行排序)
Rank rank = new Rank();//創(chuàng)建對象
System.out.println("輸入所需要排序的成績編號 , 1:數(shù)學,2:英語,3:計算機");
int major = in.nextInt();
rank.rankscore(stu,major);
//輸出排序后的成績
for(int i = 0;i < stu.length;i++) {
System.out.println("學號:"+stu[i].number+" 學生:"+stu[i].name+" 專業(yè):"+stu[i].major+" 數(shù)學:"+stu[i].math+" 計算機:"+stu[i].computer+" 英語:"+stu[i].english);
}
}
else {
System.out.println("結束,退出系統(tǒng)");
}
}
}
其中使用構造方法初始化的時已經(jīng)存入了值,因此在使用set方法輸入學生信息時其實是修改學生信息,在構造方法初始化的時候可以不用那么復雜 可直接根據(jù)數(shù)據(jù)類型 例如:
stu[0] = new Student(" "," "," ",0,0,0);
stu[1] = new Student(" "," "," ",0,0,0);
stu[2] = new Student(" "," "," ",0,0,0);
stu[3] = new Student(" "," "," ",0,0,0);
stu[4] = new Student(" "," "," ",0,0,0);
注意 在聲明局部變量的時候一定要記住初始化,否則將值傳入數(shù)組的時候會出現(xiàn)報錯
運行截圖:


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
spring?boot?2.x靜態(tài)資源會被攔截器攔截的原因分析及解決
這篇文章主要介紹了spring?boot?2.x靜態(tài)資源會被攔截器攔截的原因分析及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
java如何將int數(shù)組轉化為Integer數(shù)組
這篇文章主要介紹了java如何將int數(shù)組轉化為Integer數(shù)組,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Java SpringBoot模板引擎之 Thymeleaf入門詳解
jsp有著強大的功能,能查出一些數(shù)據(jù)轉發(fā)到JSP頁面以后,我們可以用jsp輕松實現(xiàn)數(shù)據(jù)的顯示及交互等,包括能寫Java代碼。但是,SpringBoot首先是以jar的方式,不是war;其次我們的tomcat是嵌入式的,所以現(xiàn)在默認不支持jsp2021-10-10
Maven編譯錯誤:程序包com.sun.*包不存在的三種解決方案
J2SE中的類大致可以劃分為以下的各個包:java.*,javax.*,org.*,sun.*,本文文章主要介紹了maven編譯錯誤:程序包com.sun.xml.internal.ws.spi不存在的解決方案,感興趣的可以了解一下2024-02-02
SpringBoot整合Redis實現(xiàn)熱點數(shù)據(jù)緩存的示例代碼
這篇文章主要介紹了SpringBoot中整合Redis實現(xiàn)熱點數(shù)據(jù)緩存,本文以IDEA?+?SpringBoot作為?Java中整合Redis的使用?的測試環(huán)境,結合實例代碼給大家詳細講解,需要的朋友可以參考下2023-03-03
IDEA中application.properties的圖標顯示不正常的問題及解決方法
這篇文章主要介紹了IDEA中application.properties的圖標顯示不正常的問題及解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

