为了账号安全,请及时绑定邮箱和手机立即绑定

这是我写的课后习题代码,供同学们参考

public void sortRandomString(){

List<String> stringList = new ArrayList<String>();

String str;

for(int i=0;i<10;i++){

//随机生成数字或字母并添加到str

do{

str="";

for (int x = 0; x < 10; x++) {

int j = (int) (Math.random() * 3 + 1);

switch (j) {

case 1:

// 生成随机数字

int k = (int) (Math.random() * 10);

str += k;

break;

case 2:

// 生成随机大写字母

int m = (int) (65 + Math.random() * 26);

str += (char) m;

break;

case 3:

// 生成随机小写字母

int n = (int) (97 + Math.random() * 26);

str += (char) n;

break;

}

if(Math.random()<0.1)

break;

}

}while(stringList.contains(str));

stringList.add(str);

System.out.println("List中添加了字符串:"+str);

}

System.out.println("----------------List排序前-----------------");

for (String string : stringList) {

System.out.println(("元素:"+string));

}

Collections.sort(stringList);

System.out.println("----------------List排序后-----------------");

for (String string : stringList) {

System.out.println(("元素:"+string));

}

}


public static void main(String[] args) {

// TODO Auto-generated method stub

CollectionsTest ct = new CollectionsTest();

ct.sortRandomString();

}

}


正在回答

1 回答

盖盖盖

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409774    人
  • 解答问题       4546    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

这是我写的课后习题代码,供同学们参考

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号