jeudi 5 avril 2018

How to sort array in a random way? [duplicate]

This question already has an answer here:

I've made a program that randomly sorts songs (gets a array of songs and prints the name of the singer in a different order than it get) The program get the array and creates a new array exactly like the original (so you can change the array but still use him in a diffrent place) So the program has a random number (double between 0 to 0.999 and it turns it into an eint and prints the name of the singer Which is in the same place in the array, then it changes the name of the singer (in the new array) to 1 and then again prints the name of the next singer and if he is 1 (the name of the singer) So she skips it and prints another singer. But something in the software does not work I would be happy if you try to understand what the problem is :) this is the code:

public void miunByRandom(Song[] song3){
    Song[] song6 = new Song[song3.length];
    for (int i = 0; i < song6.length; i++) {
        song6 [i] = song3[i];
    }
    System.out.println("this is your songs list sorts by random");
    for (int i = 0; i < song6.length; i++) {
        double f = Math.random();
        f = f * (song6.length+1);  
        f = (int) f ; 
        int k = (int) f;
        if (song6[k].getName() =="1") {
            i--;
        }else {
            System.out.println(song6[k].getName());
            song6[k].getName().equals("1") ;
        }
    }
}

when i try to activate the program it wrote that is a problem in line 12 and whan i check The program did not change the singer's name to 1




Aucun commentaire:

Enregistrer un commentaire