import java.util.HashMap; import java.util.Iterator; import java.util.Map; /* * 完成消息的发送功能 * 在发送消息之前,首先选择联系人所在的组(本案例分三组:好友组,亲人组,同事组) * 然后输入接收人的昵称;最后输入要发送的消息 * @2016.11.1, */ public class Message1 { private String content; private String name; public static Map<Integer,String[]> map=new HashMap<Integer,String[]>(); public Message1(String content,String name){ this.name=name; this.content=content; } public static void getMap(){ String[] friends={"岁月如歌","人生如梦","填方叶檀","心在旅途","我心飞扬"}; String[] relativesStrings={"岁月","人生","填方","旅途","飞扬"}; String[] colleagues={"张三","李四","王五","孙六"}; map.put(1, friends); map.put(2, relativesStrings); map.put(3, colleagues); } public static void nameList(int num){ getMap(); Iterator iterater=map.keySet().iterator(); while(iterater.hasNext()){ int key=(Integer) iterater.next(); if(key==num){ for(int i=0;i<map.get(key).length;i++){ System.out.println(map.get(key)[i]); } } } } public static boolean validateName(int num,String name){ boolean flag=false; Iterator iterater=map.keySet().iterator(); loop:while(iterater.hasNext()){ int key=(Integer) iterater.next(); if(key==num){ for(int i=0;i<map.get(key).length;i++){ if(name.equals(map.get(key)[i])){ flag=true; break loop; } } } } return flag; } public void printMsg(){ System.out.println("我: "+content); } } import java.util.Scanner; public class Message2 { /** * @param args */ public static void main(String[] args) { System.out.println("1:好友组 2:亲人组 3:同事组"); System.out.println("请选择接收消息者所在的分组编号:"); Scanner sc=new Scanner(System.in); int num=sc.nextInt(); Message1.nameList(num); System.out.println("请输入接收消息者的昵称:"); String name=sc.next(); boolean flag1=Message1.validateName(num, name); if(flag1){ System.out.println("请输入消息内容:"); String content=sc.next(); Message1 message=new Message1(content, name); message.printMsg(); }else{ System.out.println("您输入的昵称不存在"); } } public static double methord(byte x,double y){ return (short)x/y*2; } }
1:好友组 2:亲人组 3:同事组 请选择接收消息者所在的分组编号: 1 岁月如歌 人生如梦 填方叶檀 心在旅途 我心飞扬 请输入接收消息者的昵称: 人生如梦 请输入消息内容: 你好毒 我: 你好毒