1. 程式人生 > >學生成績錄入系統與查詢

學生成績錄入系統與查詢

package zhang;

import java.math.BigDecimal;

import java.util.Scanner;

public class str {

public static void main(String[] args) {

String[] names = {"文蓮","張廣闊","王晨晨"};

double[] score = new double[names.length];

Scanner scan = new Scanner(System.in);

while(true) {

System.out.println("請輸入1.成績錄入2.顯示成績列表3.查詢成績0.退出程式");

String cmd = scan.nextLine();

if(cmd.equals("0")){

System.out.println("大爺,下次再來啊!");

break;

}

else{

switch(cmd)

{

case"1":

for(int i=0;i<names.length;i++){

System.out.println((i+1)+"."+names[i]+"成績為:");

String str = scan.nextLine();

double score1=Double.parseDouble(str);

score[i] = score1;

}

break;

case"2":

double sum=0;

for(int i=0;i<names.length;i++){

System.out.println((i+1)+"."+names[i]+"成績為:"+score[i]);

sum = sum +score[i];

}

double avg = sum/names.length;

BigDecimal bgAvg = new BigDecimal(avg);

bgAvg.setScale(2,BigDecimal.ROUND_HALF_UP);

System.out.println("平均分為:"+bgAvg);

break;

case"3":

System.out.println("請輸入你要查詢人的姓名:");

String inputName=scan.nextLine();

for(int i = 0;i<names.length;i++){

//System.out.println(names[i]);

if(inputName.equals(names[i])){

System.out.println(names[i]+"成績為:"+score[i]);

}

}

break;

default:

System.out.println("輸入有誤哦親,請重新輸入!");

}

}

}

}

}