java编程 软件eclipse 利用面向对象的思想编写程序,实现生成一随机字母(a~z,A~Z),并输出。 30
2个回答
展开全部
class A{
public String getRandomLetter(){
String a[] = {"a","b","c","d","e","f",
"g","h","i","j","k","l",
"m","n","o","p","q","r",
"s","t","u","v","w","x",
"y","z","A","B","C","D",
"E","F","G","H","I","J",
"K","L","M","N","O","P",
"Q","R","S","T","U","V",
"W","X","Y","Z"};
Random random = new Random();
int max = 51, min = 0;
//得0-51范围内的随机数
int i = random.nextInt(max)%(max-min+1) + min;
return a[i];
}
}
class Test{
public static void main(String[] args) {
A a = new A();
System.out.println(a.getRandomLetter());
}
}
更多追问追答
追问
有错误的你在看看
追答
用记事本写的,你错误截一下
展开全部
String KeyString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuffer sb = new StringBuffer();
int len = KeyString.length();
for (int i = 0; i < length; i++) {
sb.append(KeyString.charAt((int) Math.round(Math.random() * (len - 1))));
}
return sb.toString();
追答
public class Main {
public static void main(String args[]) {
String KeyString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuffer sb = new StringBuffer();
int len = KeyString.length();
Random rand = new Random();
int randNum = rand.nextInt(len);
sb.append(KeyString.charAt(randNum));
System.out.print(sb.toString());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询