Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不使用库和排序方法的情况下洗牌字符串数组?_Java_Arrays_String_Shuffle - Fatal编程技术网

Java 如何在不使用库和排序方法的情况下洗牌字符串数组?

Java 如何在不使用库和排序方法的情况下洗牌字符串数组?,java,arrays,string,shuffle,Java,Arrays,String,Shuffle,我正在写一副52张卡片的牌。一切都很完美,但如果不使用java中的任何库和java中内置的排序方法,我就不知道如何洗牌。这是我的密码。我一直想弄明白一些事情,但到目前为止我还不能 String [] deck2=new String [52]; String[] deck=new String [52]; String suits[]={"Spades","Hearts","Diamonds","Clubs"}; String rank[]={"2","3","4","5","6","7","8

我正在写一副52张卡片的牌。一切都很完美,但如果不使用java中的任何库和java中内置的排序方法,我就不知道如何洗牌。这是我的密码。我一直想弄明白一些事情,但到目前为止我还不能

String [] deck2=new String [52];
String[] deck=new String [52];
String suits[]={"Spades","Hearts","Diamonds","Clubs"};
String rank[]={"2","3","4","5","6","7","8","9","10","Jack","King","Queen","Ace"};

for(int i=0;i<deck.length;i++){
    deck[i]=rank[i%13]+" "+"of "+suits[i/13];
    deck2[i]=deck[i];
    System.out.println(deck[i]);

}}}
String[]deck2=新字符串[52];
字符串[]组=新字符串[52];
弦乐套装[]={“黑桃”、“红桃”、“钻石”、“梅花”};
字符串排名[]={“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“杰克”、“国王”、“女王”、“王牌”};
for(int i=0;i
for(int i=0;i<500;i++){
int from=(int)(Math.random()*array.length);//从中获取第一个值的位置
int to=(int)(Math.random()*array.length);//从中获取第二个值的位置
字符串perm=array[from];//存储“from”值
array[from]=array[to];//将第一个值设置为第二个值
array[to]=perm;//将第二个值设置为第一个值
}
我不知道,为什么您不想使用java内置库,它们存在是有原因的

在没有任何内置方法的情况下创建一个随机数是很困难的。很久以前我发现了一种方法,使用float,并使其溢出

float random = 1 << 32 + 1256224; //Have a random number here, it doesn't matter

float random=1当然有可能。毕竟,库只不过是一些你可以自己编写的代码。你必须自己编写随机数生成器。这是一个简单的例子:

private static long x = System.currentTimeMillis();

public static long rndNumber() {
    x ^= (x << 21);
    x ^= (x >>> 35);
    x ^= (x << 4);
    return x < 0 ? -x : x;
}

public static void shuffle(int a[]) {
    for (int i = a.length - 1; i > 0; i--) {
        int pos = (int) (rndNumber() % a.length);
        int temp = a[i];
        a[i] = a[pos];
        a[pos] = temp;
    }
}

public static void main(String[] args) {
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    System.out.println(Arrays.toString(a));
    shuffle(a);
    System.out.println(Arrays.toString(a));
}
private static long x=System.currentTimeMillis();
公共静态长rndNumber(){
x^=(x>>35);
x^=(x 0;i--){
int pos=(int)(rndNumber()%a.length);
int temp=a[i];
a[i]=a[pos];
a[pos]=温度;
}
}
公共静态void main(字符串[]args){
INTA[]={1,2,3,4,5,6,7,8,9};
System.out.println(Arrays.toString(a));
洗牌(a);
System.out.println(Arrays.toString(a));
}

您可以使用Math.Random()我认为这个答案对你很有帮助不,我不能使用任何内置的库,比如math random或sort。它必须是我自己的。我也不能使用math random。它是一个内置的库。我自己在学习java,有人告诉我最好的学习方法是看看数组列表是如何工作的。我正在尝试使用任何java中的库。尝试自己对所有内容进行排序。这真的很好。再说一遍,为什么你不能使用它们?我编辑了我的帖子,我提供了一种解决方法,为什么不使用,它与随机数生成器一样公平,只需要n-1个步骤?@KingJames避免库不会让你学习真正的java编码,而java编码的中心是选择正确的库。对于使用Fisher-Yates shuffle,它是O(n)并且与随机数生成器一样公平,+1
float random = 1 << 32 + 1256224;
String [] deck2=new String [52];
String[] deck=new String [52];
String suits[]={"Spades","Hearts","Diamonds","Clubs"};
String rank[]={"2","3","4","5","6","7","8","9","10","Jack","King","Queen","Ace"};

for(int i=0;i<deck.length;i++){
    deck[i]=rank[i%13]+" "+"of "+suits[i/13];
    deck2[i]=deck[i];
    System.out.println(deck[i]);

}
for (int i = 0; i < 500; i++) {
    int from = (int) (Math.random() * array.length); //The place to get the first from
    int to = (int) (Math.random() * array.length); // The place to get the second from
    String perm = array[from]; // Store the "from" value
    array[from] = array[to]; // Set the first value to the second
    array[to] = perm; // Set the second value to the first
}
private static long x = System.currentTimeMillis();

public static long rndNumber() {
    x ^= (x << 21);
    x ^= (x >>> 35);
    x ^= (x << 4);
    return x < 0 ? -x : x;
}

public static void shuffle(int a[]) {
    for (int i = a.length - 1; i > 0; i--) {
        int pos = (int) (rndNumber() % a.length);
        int temp = a[i];
        a[i] = a[pos];
        a[pos] = temp;
    }
}

public static void main(String[] args) {
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    System.out.println(Arrays.toString(a));
    shuffle(a);
    System.out.println(Arrays.toString(a));
}