❶ 用java程序冒泡排序算法把一组打乱顺序的数字从小到大排列并打印出来
publicclassMaoPao
{
publicstaticvoidmain(Stringargs[])
{
int[]arr={2,1,3,4,6,5,7,8,9,0,10};
//N是数组的元素个数,这样无论多少个数,直接修改arr中的元素就行了,
//不需要调整循环次数
intN=arr.length;
inttemp=0;
//冒泡排序:每次把最大的放到最后,N-i是因为第i次排序之后,
//数组arr的最后i个数已经是按照大小顺序的了,所以不需要再排序了
//比如第一次排序之后,最后一个数肯定是最大的,下一次只需要排前9个就行了。
for(inti=1;i<N;++i)
{
for(intj=0;j<N-i;++j)
{
//如果前面的数比后面的大,则不是按照顺序的,因此要交换
if(arr[j]>arr[j+1])
{
temp=arr[j];//交换2个数
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(inti=0;i<N;++i)//输出排序后的结果
{
System.out.print(arr[i]+"");
}
}
}
忘采纳。
❷ java中怎么把一个数组元素随机打乱顺序
晕.不用那么麻烦.
先转化为list(为什么不一开始就用List呢?)
例:
String[] arr = new String[] {"1", "2"};
List list = Arrays.asList(arr);
直接调用shuffle,就是随机回排序
例:Collections.shuffle(list);
直接输出就是你想要答的结果
❸ 如何在JAVA中随机打乱输出一组数字而不重复
哥给你完善下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class A6 {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
boolean same = false;
int num1 = 0;
int num2 = 0;
while (!same) {// if the numbers which user enter are the same,do it
//碰没睁 again
try {
System.out.print("enter a number: ");//笑岁 ask user a number
num1 = Integer.parseInt(in.readLine());
System.out.print("enter another number: ");// ask user another
// number
num2 = Integer.parseInt(in.readLine());
if (num1 != num2) {
same = true;// if the number are different,continue the next part and don't need to enter again
} else {
System.out.println("two numbers is same.");// if not , ask user to enter again
}
} catch (NumberFormatException nfc) {// text the enter is a number or not,if not,ask user enter again
System.out.println("not a number");// show user the reason why get false
}
}
//Add starts
List<Integer>察清 list = new ArrayList<Integer>();
int max = num1 > num2? num1: num2;
int min = num1 < num2? num1: num2;
Random rand = new Random();
while(list.size() < (max - min + 1)){
int value = min + rand.nextInt(max - min + 1);
if(value >= min && value <= max && !list.contains(value)){
list.add(new Integer(value));
}
}
//Calcuate sum for each pair
int sum[] = new int[list.size() - 1];
for(int i = 0; i < list.size() - 1; i++){
sum[i] = list.get(i).intValue() + list.get(i+1).intValue();
}
Arrays.sort(sum); //升序排列结果
System.out.println("Max pair sum is: " + sum[sum.length - 1]);
System.out.println(list.toString());
//Add ends
}
}
--------------测试
enter a number: 6
enter another number: -3
Max pair sum is: 10
[3, 4, 6, -2, 0, 2, -1, 5, 1, -3]
❹ java怎么打乱一个由键盘录入规定数组长度以及数据的一维数组并遍历出来
首先键盘族肆录入可以用scanner类实现控制台输入,然后打乱数组可以用random类产生随机数来控制输入的数存入随机的数组位置,这个地方要注意随机数不可重复,要兆裤轿控制范围在纯配定义的数组长度内,最后遍历直接一个for循环。
❺ Java如何打乱集合中的元素
package com.yii;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class CollectionShuffle {
public static void main(String[] argv) throws Exception {
ArrayList<String> obj = new ArrayList<String>();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add("D");
obj.add("E");
obj.add("F");
Collections.shuffle(obj);
System.out.println(obj);
}}
❻ java【不用list】如何将数组元素顺序打乱
这好办得很嘛, 假如长度为10, 从10内随机取一个数,把改下标的值和下标0互换, 换个10次或者更高就行了
❼ java里怎样打乱(洗牌)一个数组
//给你个思路:给要给随机值,该随机值在索引范围内,然后从索引值里面取元素,在该元素对应
//的位置,进行二次随机取其他元素,然后进行交换,就可以了!
//还有更简单的办法,java早就想到了这一点,在集合工具里面就提供该方法,其实他内部也是
//这样的思路,用随机值进行交换!
importjava.util.*;
publicclassArrayListTest{
publicstaticvoidmain(String[]args){
Integer[]arr=newInteger[10];
for(inti=0;i<arr.length;i++){
arr[i]=i+1;
}
System.out.println(" 原序:"+Arrays.toString(arr)+" ");//原数组!
List<Integer>list=Arrays.asList(arr);//借用集合帮忙排序!
for(Integera:list){
Collections.shuffle(list);//随机排序!
System.out.println("随机:"+Arrays.toString(arr));//排序后!
}
}
}
//数字或者数量,你可以随意修改,二维数组也可以照用!
❽ 关于洗牌算法,请用JAVA编写,定义一个数组,储存1-52以内的数,打乱顺序输出!
import java.util.Enumeration;
import java.util.Hashtable;/**
* 7. * 乱序扑克牌 洗牌方法 8. * 9. *
*
* @author virture 10. * 11.
*/
public class Cards { Hashtable htMember = new Hashtable();// 放置扑克牌的Hash表 public Cards() { } public void put(String card) {
htMember.put(card, card);
} public void get() {
System.out.println("你拿到的牌是:");
Enumeration RLKey = htMember.keys();
while (RLKey.hasMoreElements()) {
String accKey = RLKey.nextElement().toString();// 取HashTable中的关键字词
System.out.print((String) htMember.get(accKey) + ",");
}
} public static void main(String[] args) {
String[] cards = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
String[] kinds = { "黑桃", "红桃", "梅花", "方块" }; Cards cardList = new Cards(); String suit;// 当前选中牌的类型
String face;// 当前选中牌
int randomCardNum = 52;// 当前随机取牌的个数,记住不能大于全部牌52张 while (true) {
suit = kinds[Math.round((float) Math.random() * (kinds.length - 1))];
face = cards[Math.round((float) Math.random() * (cards.length - 1))]; cardList.put(suit + face);
if (cardList.htMember.size() >= randomCardNum
&& cardList.htMember.size() <= 52) {
break;
}
}
cardList.get();
}
}
❾ 请问Java如何用math. random方法打乱输出的字符
String ss = "1,2,3,4,5,6,7,8,9";
System.out.println(ss);
String[] strs = ss.split(",");
Random a = new Random();
int i = strs.length;
while (i > 0){
int num = a.nextInt(strs.length);
while(num < strs.length && strs[num]==""){
num++;
if(num == strs.length){
num = 0;
}
}
System.out.print(strs[num]+" ");
strs[num] = "";
i--;
}
可以考虑随机数,输出之后就给空渣磨;
不给空的话,可以记录已经输出之后的num值
可以考虑用set 保存数字的李轮集合,这样原数组的内容是没清空的
String ss = "1,2,3,4,5,6,7,8,9";
System.out.println(ss);
String[] strs = ss.split(",");
Random a = new Random();
Set set= new HashSet();
int i = strs.length;
while (i >哪梁信 0){
int num = a.nextInt(strs.length);
while(num < strs.length && set.contains(num)){
num++;
if(num == strs.length){
num = 0;
}
}
System.out.print(strs[num]+" ");
set.add(num);
i--;
}
❿ 怎么用java代码把打乱的数字1到13按从大到小排序
那要看你的13个数字是怎么输入的了
如果直接是在数组里,用冒泡排序
int a[]={1,10,7,8,5,3,2,4,6,11,13,12,9};
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i]>a[j]) {
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for (int i : a) {
System.out.print(i+" ");
}