首页 > 编程语言 > 详细

java黄金点游戏

时间:2017-12-27 13:11:06      阅读:233      评论:0      收藏:0      [点我收藏+]

在同学的帮组下用java写的黄金点游戏,写的不是很好,希望各位多多指点。本来想使用swing来做几个按钮的,但是对这个确实不是很懂,还是放弃了,采用了控制台输入的方式。主要是

使用hashmap来存储参加游戏的name和每次输入的数据。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package demo2;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
 
public class GoldPoint {
public static void main(String[] args){
    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 peopleNum;//参加的人数
    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();
    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;
    }
    aver=sum/peopleNum*0.618;
    System.out.println("aver="+aver);
    this.findWinner(inputMap, scoreMap, aver);
    this.show(scoreMap);
    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;
        }
            aver=sum/peopleNum*0.618;
            System.out.println("aver="+aver);
            this.findWinner(inputMap, scoreMap, aver);
            this.show(scoreMap);
        System.out.println("第"+(i+2)+"轮结束");
    }     System.out.println("游戏结束");
}
//找出每次分数最接近黄金点的 和最远的 最接近的加一分 最远的减一分 其余加零分(可能有相同的)
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){//寻找最近
            temp=temp1;
             latest.clear();
             latest.add(key);
        }else if(temp==temp1){
            latest.add(key);
        }
        if(temp0<temp1){//寻找最远
            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+1;
            scoreMap.put(key, score);
            }
        if(this.containList(key, farthest)){
            score=score-1;
            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("得分情况:");
    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);
    }
}
 
}

java黄金点游戏

原文:https://www.cnblogs.com/7inkoo/p/8124834.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!