gpt4 book ai didi

java - 如何将生成的 2 位随机数转换为字母

转载 作者:行者123 更新时间:2023-11-30 10:52:15 26 4
gpt4 key购买 nike

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 位随机数转换为字母例如在上面代替 12 我想要 GB .......像那样....任何帮助.....提前致谢....我有一个截止日期来完成这个....所以请帮助我......

最佳答案

请使用下面的代码,

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);
}

关于java - 如何将生成的 2 位随机数转换为字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34411592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com