package com.nusecond.Code;
import java.util.*;
public class Code1 {
    public static void main(String args[]){
         HashMap<Integer, Character> hmap = new HashMap<Integer, Character>();
         hmap.put(0, 'D');
         hmap.put(1, 'G');
         hmap.put(2, 'B');
         hmap.put(3, 'Q');
         hmap.put(4, 'P');
         hmap.put(5, 'M');
         hmap.put(6, 'R');
         hmap.put(7, 'Y');
         hmap.put(8, 'Z');
         hmap.put(9, 'I');

Iterator<Integer> keySetIterator = hmap.keySet().iterator();
while(keySetIterator.hasNext()){
int key=keySetIterator.next();


System.out.println("key:" + key +  " value: "  +hmap.get(key));

}
for(int j=1001;j<9999;j++);
Random random=new Random();
int res=random.nextInt(100);
System.out.println(res);
    }
    }


输出:

key:0 value: D
key:1 value: G
key:2 value: B
key:3 value: Q
key:4 value: P
key:5 value: M
key:6 value: R
key:7 value: Y
key:8 value: Z
key:9 value: I
12




在这里我试图将生成的2位数随机数转换为字母
例如在上面的位置,我要GB代替12。
像那样....
任何帮助。。。谢谢。
我有完成任务的截止日期。
所以请帮助我.......

最佳答案

请使用以下代码,

HashMap<Integer, Character> hmap = new HashMap<Integer, Character>();
hmap.put(0, 'D');
hmap.put(1, 'G');
hmap.put(2, 'B');
hmap.put(3, 'Q');
hmap.put(4, 'P');
hmap.put(5, 'M');
hmap.put(6, 'R');
hmap.put(7, 'Y');
hmap.put(8, 'Z');
hmap.put(9, 'I');

Iterator<Integer> keySetIterator = hmap.keySet().iterator();
while(keySetIterator.hasNext()){
int key=keySetIterator.next();


System.out.println("key:" + key +  " value: "  +hmap.get(key));

}
for(int j=1001;j<9999;j++);
Random random=new Random();
int res=random.nextInt(100);
System.out.println(res);
String s = "";
s = String.valueOf(res);
String result="";

for(int i=0;i<s.length();i++)
{
    result+=hmap.get(Integer.valueOf(String.valueOf(s.charAt(i))));
}

System.out.println(result);
        }

09-06 10:20