已知的三个数(比如,a,b,c),随机显示其中一个

ruisheng_412 2009-08-05 09:36:15
已知的三个数(比如,a,b,c),随机显示其中一个
...全文
228 13 打赏 收藏 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinpinle 2009-08-05
  • 打赏
  • 举报
回复
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 3; i++) {
list.add(i);
}
System.out.println(list.toString());
Collections.shuffle(list);
System.out.println(list.toString());
}
sinpinle 2009-08-05
  • 打赏
  • 举报
回复
你先吧数放入一个list,你可以用collection 的shuffle方法
gukuitian 2009-08-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 gukuitian 的回复:]
放list里,
for(int i=0;i <list.size();i++)
{
    System.out.println(list.get(new Random().nextInt(i+1)));
    list.remove(new Random().nextInt(i+1));

}
[/Quote]

public static void main(String args[])
{
List list=new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
for(int i=list.size();i>0;i--)
{
int a=new Random().nextInt(i==0?1:i);
System.out.println(list.get(a));
list.remove(a);
}
}
badperson1 2009-08-05
  • 打赏
  • 举报
回复
丫的,6楼的!首先安你这种方法得出的序列根本不随机(每个序列的概率不一样),其次楼主没说能否重复~所以楼主是白吃~
gukuitian 2009-08-05
  • 打赏
  • 举报
回复
放list里,
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(new Random().nextInt(i+1)));
list.remove(new Random().nextInt(i+1));

}
crazystone83 2009-08-05
  • 打赏
  • 举报
回复
	/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 1 ;
int b = 3;
int c = 5;
ArrayList list = new ArrayList();
list.add(a);
list.add(b);
list.add(c);
int randomValue = 0;
Random random = new Random();
for(int i=0;i<=20;i++){
randomValue = random.nextInt(3);
// System.out.println(randomValue);
System.out.println(list.get(randomValue));
}
}
badperson1 2009-08-05
  • 打赏
  • 举报
回复
3个数的不同顺序排序一共6种,可以在1~6之间随机生成一个数,然后选择对应的序列,当然这种方法只适合数目少的序列……
sungoshawk 2009-08-05
  • 打赏
  • 举报
回复
public class ClassTest {
public static void main(String[] args) {
String[] arr = {"a" , "b" , "c"} ;
for(int i = arr.length ; i >= 1 ; i--){
int rand = (int)(Math.random() * i) ;
System.out.println(arr[rand]) ;

if(rand != arr.length - 1){
String str ;
str = arr[rand] ;
arr[rand] = arr[i -1] ;
arr[i -1] = str ;
}
}
}
}

看看这个行吗
YY_MM_DD 2009-08-05
  • 打赏
  • 举报
回复
把他设想成一个三角行的图..
然后去遍历这张图
ruisheng_412 2009-08-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gukuitian 的回复:]
一共三个,还随机显示三个?不同顺序吗?
[/Quote]

是不同的顺序
gukuitian 2009-08-05
  • 打赏
  • 举报
回复
一共三个,还随机显示三个?不同顺序吗?
ruisheng_412 2009-08-05
  • 打赏
  • 举报
回复
如果我要随机显示着三个数?不是显示其中一个。
霜之哀伤 2009-08-05
  • 打赏
  • 举报
回复
放到一个数组里,
生成一个0-2的随机数,然后去数组里取,然后显示。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧