lundi 6 mai 2019

Is there a way to undo a method which randomly picks out elements and prints them in a random order with a new method?

My assignment was to write code that took a sentence and scrambled it, then printed it. After that it was supposed to take the same string and unscramble it. I found a way to scramble, but a method to undo the aforementioned scrambled is still lost on me. Would appreciate all, if any advice on a possible solution. Thank you.

System.out.println("Enter a Sentence");
original= scan.nextLine();

String a[] = original.split("");

for (int i=0; i<a.length; i++) {                                    
    int pos = ran.nextInt(a.length);                    
        temp = a[i];                                         
        a[i] = a[pos];                               
        a[pos] = temp; 
    }                                                                   
   for (String element: a) {                                                                    
    System.out.print(element);                                                        
}        
}




Aucun commentaire:

Enregistrer un commentaire