java 期末考试_java 期末考试复习

//yuec2 yue cheng

packageexam1;//import java.io.File;

importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.util.Scanner;public classScoreBoard {

String[] players;int[][] playerScores;

Scanner input= newScanner(System.in);//do not change this method

public static voidmain(String[] args) {

ScoreBoard scoreBoard= newScoreBoard();

scoreBoard.readScores("GameScores.txt");

scoreBoard.showScores(scoreBoard.getMenuChoice());

}//do not change this method

intgetMenuChoice() {

System.out.println("*** Score Board ***");

System.out.println("1. List all the scores of a player");

System.out.println("2. Find a player's average score");

System.out.println("3. Find all highest scores");

System.out.println("4. Exit");int choice =input.nextInt();

input.nextLine();returnchoice;

}voidreadScores(String filename) {//write your code here

try{int counts=0;

Scanner sc= new Scanner((new FileReader("GameScores.txt")));while(sc.hasNextLine()) {

sc.nextLine();

counts++;}

players= newString[counts];

playerScores= new int[counts][];

sc= new Scanner((new FileReader("GameScores.txt")));for(int i=0; i< counts; i++) {

String thisline=sc.nextLine();

players[i]= thisline.split(":")[0].trim();

String[] temp= thisline.split(":")[1].trim().split(",");int size =temp.length;

playerScores[i]= new int[size];for (int j = 0; j < size; j++) {

playerScores[i][j]=Integer.valueOf(temp[j].trim());

}

}

}catch(FileNotFoundException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}void showScores(intchoice) {//write your code here

if (choice == 1) {

String name=input.nextLine();

findPlayerScores(name);

}else if (choice == 2) {

String name=input.nextLine();

findPlayerAverageScore(name);

}else if (choice == 3) {

findAllHighestScores();

}else if (choice == 4) {

System.out.println("Good bye!");

}return;

}int[] findPlayerScores(String name) {//write your code here

int index = -1;for (int i = 0; i < players.length; i++) {//System.out.println("players[i] = " + players[i]);

if (players[i].toLowerCase().equals(name.toLowerCase())) index =i;

}//corner case

if (index == -1) return null;//System.out.println("index = " + index);//System.out.println(players[index] +"'s scores\n");

for (int j = 0; j < playerScores[index].length; j++) {

System.out.println(playerScores[index][j]);

}returnplayerScores[index];

}floatfindPlayerAverageScore(String name) {//write your code here

int[] scores =findPlayerScores(name);float sum = 0;for(int i = 0; i < scores.length; i++) {

sum+=scores[i];

}

System.out.printf("%s's average score is %.2f", name, sum /scores.length);return sum /scores.length;

}int[] findAllHighestScores() {//write your code here

int[] highestScores = new int[players.length];int max =Integer.MIN_VALUE;for (int i = 0; i < players.length; i++) {int size =playerScores[i].length;for (int j = 0; j < size; j++) {

max=Math.max(max, playerScores[i][j]);//System.out.println("max = " + max);

}

highestScores[i]=max;

max=Integer.MIN_VALUE;

System.out.println(highestScores[i]);

}returnhighestScores;//return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值