Use pure Java Golden dot game

Source: Internet
Author: User

Recently use pure Java to write the Golden dot game, writing is not very good, I hope you have a little bit of pointing. I wanted to use swing to do a few buttons, but for this really not very understand, or give up, the use of the console input method. is mainly

Use HashMap to store the name of the game and the data entered each time.

The code is as follows:

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> ();//Deposit Input score HashMap <String,Double> scoremap=new hashmap<string,double> ();//Deposit score String name= "";D ouble Inputscore;int peoplenum;//the number of participants int time;//the number of rounds Double sum=0.0;double aver=0.0; Scanner scan=new Scanner (system.in); The parameter object is the flow System.out.println the system comes in ("Enter Number of participants:");p eoplenum=scan.nextint (); SYSTEM.OUT.PRINTLN ("Input required several rounds:"); Time=scan.nextint (); for (int i=0;i<peoplenum;i++) {System.out.println ("Please enter" + ( I+1) + "Participant's name:"); Name=scan.next (); System.out.println ("Please enter the first round of the score:"); inputscore=scan.nextdouble (); Inputmap.put (name, Inputscore); Scoremap.put (name, (double) 0);//Initialize Scoremap Sum+=inputscore;} aver=sum/peoplenum*0.618; System.out.println ("aver=" +aver); This.findwinner (InputMap, Scoremap, aver); This.show (SCOREMAP); System.out.println ("First round End"); for (int i=0;i<time-1;i++) {sum=0.0; System.out.println ("Please enter the score of the" + (i+2) + "wheel:"); Iterator iter = Inputmap.entryset (). Iterator (); while (Iter.hasnext ()) { Map.entry entry0 = (map.entry) iter.next (); String key = (string) entry0.getkey (); SYSTEM.OUT.PRINTLN (key+ "input" + (i+2) + "Round score:");D ouble score =scan.nextdouble (); Inputmap.put (key, score);//Replace the previous fractional sum +=score;} aver=sum/peoplenum*0.618; System.out.println ("aver=" +aver); This.findwinner (InputMap, Scoremap, aver); This.show (SCOREMAP); System.out.println ("First" + (i+2) + "round End");}} Find out where each score is closest to the golden point and farthest from the nearest plus one and the furthest minus one cent the rest plus 0 points (possibly the same) 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 ();D ouble input = (Double) entry.getvalue (); String key0 = (string) entry.getkey (); Latest.add (Key0); Farthest.add (Key0);//iter.hasnext (); Temp0=temp=math.abs ( Aver-input);//Traverse Mapwhile (Iter.hasnext ()) {entry = (map.entry) iter.next (); String key = (string) entry.getkey (), input = (Double) entry.getvalue ();D ouble temp1=math.abs (aver-input), if (temp> TEMP1) {//search for recent temp=temp1; Latest.clear (); Latest.add (key);} else if (TEMP==TEMP1) {latest.add (key);} if (TEMP0&LT;TEMP1) {//Search for farthest temp0=temp1;farthest.clear (); Farthest.add (key);} else if (TEMP0==TEMP1) {farthest.add (key);}} Implementation of the Bonus ITER = Scoremap.entryset (). iterator (); while (Iter.hasnext ()) {Map.entry entry0 = (map.entry) iter.next (); String key = (string) entry0.getkey ();D ouble 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 ("score Condition:"); Iterator iter = Scoremap.entryset (). iterator (); while (Iter.hasnext ()) {Map.entry entry0 = (map.entry) iter.next (); String key = (string) entry0.getkey ();D ouble score = (Double) entry0.getvalue (); System.out.println (key+ ":" +score);}}}

  

Use pure Java Golden dot game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.