第一次实训任务
码云地址:https://gitee.com/tzy123/shixun1
组员:黄硕(学号:16012118)
田朝阳(学号:16012122)
实训任务:
N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值。提交的数字最靠近G(取绝对值)的同学得到N分,离G最远的同学得到-2分,其他同学得0分。记录每一次游戏每名同学的数字和分数。
编程过程:
设计思路:
首先在游戏前确定游戏初始分值,与平均值最近的分值n,与平均值最远的分值-2,
然后编写游戏菜单,让游戏看起来清晰
输入姓名与成绩后,计算所有成绩的平均值*0.618,然后从第一个人开始与平均值*0.618作比较,选出最大值和最小值输出,同时输出所得分数
最后通过菜单退出整个游戏
代码:
import java.util.Scanner; public class HJD { static Person persons[]; private static int SCORE_START=0; private static int SCORE_INCRE=2; //减分 private static int SCORE_OUTCRE='n'; //加分 public static void main(String[] args) throws InterruptedException{ System.out.println("游戏选项:");
//菜单 while (true) { System.err.println("1、开始游戏\n2、查看当前游戏结果\n3、游戏设置\n4、退出\n**请在开始游戏前按照游戏规则进行游戏设置"); Scanner scanner=new Scanner(System.in); int choice=scanner.nextInt(); switch (choice) { case 1: welcome(); break; case 2: showMessage(); break; case 3: gameSetting(); break; case 4: System.out.println("欢迎下次使用!程序即将退出!"); Thread.currentThread().sleep(2000); System.exit(0); break; default: break; } } } private static void gameSetting() { // TODO Auto-generated method stub System.out.println("请输入玩家初试分数:"); Scanner scanner=new Scanner(System.in); SCORE_START=scanner.nextInt(); System.out.println("请输入玩家请分别输入扣分、加分分数:"); SCORE_INCRE=scanner.nextInt(); SCORE_OUTCRE=scanner.nextInt(); System.out.println(SCORE_OUTCRE); System.out.println("设置完成,可点击1开始游戏"); } private static void welcome() { System.out.println("游戏默认初始玩家的分数为0"); System.out.print("请输入游戏人数: "); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); creatPlayer(n); } private static void creatPlayer(int n) { // TODO Auto-generated method stub persons = new Person[n]; for (int i = 0; i < persons.length; i++) { persons[i] = new Person(); } for (int i = 0; i < n; i++) { Scanner scanner = new Scanner(System.in); int temp = 0; temp = i + 1; System.out.print("请输入第" + temp + "个玩家的姓名:"); persons[i].setName(scanner.next()); persons[i].setScore(SCORE_START); } System.out.println("****输入完成****"); try { playGame(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void playGame() throws InterruptedException { System.out.println("****游戏开始****"); for (int i = 0; i < persons.length; i++) { Scanner scanner = new Scanner(System.in); System.out.print("请" + persons[i].getName() + "输入数值:"); persons[i].setNum(scanner.nextInt()); } int numG = getG(); int recordMax = 0; int max = Math.abs(persons[0].getNum() - numG); for (int i = 0; i < persons.length; i++) { if (max < (Math.abs(persons[i].getNum() - numG))) { max = Math.abs(persons[i].getNum() - numG); recordMax = i; } } int min = Math.abs(persons[0].getNum() - numG); int recordMin = 0; for (int i = 0; i < persons.length; i++) { if (min > Math.abs(persons[i].getNum() - numG)) { min = Math.abs(persons[i].getNum() - numG); recordMin = i; } } System.out.println("******************************"); System.out.println("详细信息如下:"); persons[recordMin].setScore(persons[recordMin].getScore()+SCORE_OUTCRE); persons[recordMax].setScore(persons[recordMax].getScore()-SCORE_INCRE); showMessage(); System.out.println("G值为:" + numG); System.out.println("恭喜玩家" + persons[recordMin].getName()+"赢得本轮比赛,加"+SCORE_OUTCRE+"分"); System.out.println("玩家" + persons[recordMax].getName() + "输了,减两分"); System.out.println("******************************"); } private static int getG() { // TODO Auto-generated method stub int sum = 0; for (int i = 0; i < persons.length; i++) { sum += persons[i].getNum(); } return (int) ((sum / persons.length) * 0.618); } private static void showMessage() { // TODO Auto-generated method stub System.out.println(); System.out.println("玩家" + "\t" + "输入的数"+"\t"+"得分"); for (int i = 0; i < persons.length; i++) { System.out.println(persons[i].getName() + "\t" + persons[i].getNum()+"\t"+persons[i].getScore()); } System.out.println(); } }
public class Person { private String name; private int num; private int score; public Person() { // TODO Auto-generated constructor stub super(); } public Person(String tname,int tnum,int tscore) { // TODO Auto-generated constructor stub super(); name=tname; num=tnum; score=tscore; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
实验结果:
第二种方法:可以进行累加
import java.util.*; public class GoldPoint { private static int peopleNum; //声明玩家人数 public static void main(String[] args) throws InterruptedException{ GoldPoint gd=new GoldPoint(); gd.goldPoint(); } public void goldPoint(){ HashMap<String,Double> inputMap=new HashMap<String,Double>();//存入输入分数 HashMap<String,Double> scoreMap=new HashMap<String,Double>();//存入分数 String name=""; Double inputScore; int time;//进行轮数 Double sum=0.0; Double aver=0.0; Scanner scan=new Scanner(System.in); //参数对象是系统进来的流 System.out.println("请玩家输入参加的人数:"); peopleNum=scan.nextInt(); System.out.println("请玩家输入需要进行几轮:"); time=scan.nextInt(); System.out.println(); System.out.println("设置完成,游戏开始@_@"); System.out.println(); for(int i=0;i<peopleNum;i++){ System.out.println("请输入第"+(i+1)+"个参加者的姓名:"); name=scan.next(); System.out.println("请输入第一轮的分数:"); inputScore=scan.nextDouble(); inputMap.put(name, inputScore); scoreMap.put(name,(double) 0);//初始化scoreMap sum+=inputScore; } System.out.println("***输入完成***"); System.out.println("******************************"); System.out.println(); aver=sum/peopleNum*0.618; System.out.println("本轮G值"+aver); System.out.println(); this.findWinner(inputMap, scoreMap, aver); this.show(scoreMap); System.out.println("第一轮结束"); System.out.println("******************************"); for(int i=0;i<time-1;i++){ sum=0.0; System.out.println("请玩家输入第"+(i+2)+"轮的分数:"); Iterator iter = inputMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry0 = (Map.Entry) iter.next(); String key = (String) entry0.getKey(); System.out.println("请玩家"+key+"输入第"+(i+2)+"轮分数:"); Double score =scan.nextDouble(); inputMap.put(key, score);//替换掉之前的分数 sum+=score; } System.out.println("***输入完成***"); System.out.println("******************************"); System.out.println(); aver=sum/peopleNum*0.618; System.out.println("本轮G值"+aver); System.out.println(); this.findWinner(inputMap, scoreMap, aver); this.show(scoreMap); System.out.println("第"+(i+2)+"轮结束"); System.out.println("******************************"); } System.out.println(); System.out.println("欢迎下次使用!程序即将退出!"); System.exit(0); } //找出每次分数最接近黄金点的 和最远的 最接近的加人数分值 最远的减二分 其余零分(可能有相同的) public void findWinner(HashMap<String,Double> inputMap,HashMap<String,Double> scoreMap,Double aver){ Double temp; Double temp0; List<String> latest=new ArrayList<String>(); List<String> farthest=new ArrayList<String>(); Iterator iter = inputMap.entrySet().iterator(); Map.Entry entry = (Map.Entry) iter.next(); Double input = (Double) entry.getValue(); String key0 = (String) entry.getKey(); latest.add(key0); farthest.add(key0); //iter.hasNext(); temp0=temp=Math.abs(aver-input); //遍历map while (iter.hasNext()) { entry = (Map.Entry) iter.next(); String key = (String) entry.getKey(); input = (Double) entry.getValue(); Double temp1=Math.abs(aver-input); if(temp>temp1){//寻找距离G值最近的 temp=temp1; latest.clear(); latest.add(key); }else if(temp==temp1){ latest.add(key); } if(temp0<temp1){//寻找距离G值最远的 temp0=temp1; farthest.clear(); farthest.add(key);} else if(temp0==temp1){ farthest.add(key); } } //实现加分 iter = scoreMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry0 = (Map.Entry) iter.next(); String key = (String) entry0.getKey(); Double score =(Double) entry0.getValue(); if(this.containList(key, latest)){ score=score+peopleNum; scoreMap.put(key, score); } if(this.containList(key, farthest)){ score=score-2; scoreMap.put(key, score); } } } public boolean containList(String str,List<String> list){ for(int i=0;i<list.size();i++){ if(str.equals(list.get(i))){ return true; } } return false; } public void show(HashMap<String,Double> scoreMap){ System.out.println("得分情况:"); System.out.println("玩家" + ":\t" +"得分"); Iterator iter = scoreMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry0 = (Map.Entry) iter.next(); String key = (String) entry0.getKey(); Double score =(Double) entry0.getValue(); System.out.println(key+":"+score); } } }
运行结果:
总结:
代码还有不足之处,每次游戏之前需手动输入减分,而不能自动输出n,每次游戏之前需提前设置,无法自动根据人数的变化,自动输出得分。
队友评价:
思路很灵活,很负责任,总有很多新奇的想法,很有责任感。
代码行数 | 博客字数 | |
第一次实训任务 | 300 | 230 |