实训一

组员:16012118 黄硕(领航员)、16012122田朝阳(驾驶员)

本次作业GIT的提交地址:https://gitee.com/tzy123/shixun1

结对编程过程照片:

 

本次实训任务:

黄金点

阿超的课都是下午两点钟,这时班上不少的同学都昏昏欲睡,为了让大家兴奋起来,阿超让同学玩一个叫“黄金点”的游戏:

N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值。提交的数字最靠近G(取绝对值)的同学得到N分,离G最远的同学得到-2分,其他同学得0分。记录每一次游戏每名同学的数字和分数。

 

本次作业的解题思路:由题目可知,最基本的是必须要从控制台输入,即引用SCANNER类,计算公式很简单,用输入数字综合除以人数在乘以0.618就可以了,得到G值。用条件语句比较距离G值最近的和最远的数,再给提交的数字最靠近G的同学离G最远的同学实现加分。

代码如下:

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); 
}
//找出每次分数最接近黄金点的 和最远的 最接近的加人数分 最远的减2分 其余零分
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);
    }
}
 
}

 

 运行结果如下:

 

 

小结感受:我觉得结对编程有1+1>2的效果,因为一个人的思路总是有限的,而且有的时候可能想到了某种实现方法或者有了一点思路却理不清楚的时候伙伴真的很重要,进度也会比一个人做快很多,比如我们俩做第一个程序的时候,想要更改加分和做循环的时候她的想法和实施速度都比我快,而且在做的过程中了解到对方的思路和想法,真的会有不一样的收获和惊喜。另外:本来我俩最开始写的代码是下面的那个,但由于下面的代码只能进行一轮游戏,我俩改了很多次也没能实现循环进行多轮游戏,觉得不太符合题目要求,所以我们重新写了上面的代码,但是它的菜单界面做的很好,可惜我俩试了很多次也没能加到上面的代码中。

汉堡包评价:合作伙伴的优点:我觉得她想法很新颖,动手能力很强,也很聪明;

      合作伙伴的缺点:有点懒惰,有的东西总觉得差不多就行;

      希望她提高的地方:希望我的小伙伴可以在勤劳一点,对自己的要求可以在高那么一点,因为你可以更优秀,我们可以做的更好。

 

我俩最开始的代码:

import java.util.Scanner;
public class Huangjindian {
    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;
    } 
}

运行结果:

 

posted @ 2018-12-13 17:46  demi111  阅读(193)  评论(0编辑  收藏  举报