首页 > 学院 > 开发设计 > 正文

小白笔记------------------Map的用法

2019-11-08 02:50:07
字体:
来源:转载
供稿:网友
package Guess_Numbers;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;public class Guess_Numbers {	static Map<String,Integer> map1 = new HashMap<String,Integer>();	static Map<String,Integer> map2 = new HashMap<String,Integer>();	static Map<String,Integer> map3 = new HashMap<String,Integer>();	public void init1(){		map1.put("6403", 2);		map1.put("3659", 2);		map1.put("0197", 1);		map1.put("8964", 1);		map1.put("5830", 22);		map1.put("1386", 2);	}	public void init2(){		map2.put("3729", 20);		map2.put("1697", 2);		map2.put("7812", 2);		map2.put("4129", 20);		map2.put("5268", 1);		map2.put("2931", 3);	}	public void init3(){		map3.put("3419", 2);		map3.put("8345", 2);		map3.put("5974", 11);		map3.put("6723", 1);		map3.put("7081", 1);		map3.put("9837", 2);	}	public void get_answer(Map<String , Integer> map){		String answer = "";		int count = 0;		int result = 0;		for(int i = 1234;i < 9999;i++){			String s  = i + "";			count = 0;			for (String key : map.keySet()) {				 result = 0;				   for(int j = 0; j < 4;j++){					   if(s.charAt(j) == key.charAt(j)){						   result++;						   					   }					   //System.out.PRintln(result);				   }				   result = result * 10;				   for(int j = 0;j < s.length();j++){					   for(int k = 0;k < key.length();k++){						   if(s.charAt(j) == key.charAt(k) && k!=j){							   result++;							  // System.out.println(result);						   }					   }				   }				   //System.out.println(result);				   if(result == map.get(key)){					   count++;					   //System.out.println(count);				   }				   			 }			if(count == 6){				 answer = s;				 break;		   }		   			 			 		}		System.out.println(answer);	}			public static void main(String[] args) {		Guess_Numbers one  = new Guess_Numbers();		one.init1();		one.get_answer(map1);		one.init2();		one.get_answer(map2);		one.init3();		one.get_answer(map3);			}		}包含了遍历和赋值
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表