Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Shuffle.java--help!

807569Jul 16 2006 — edited Jul 17 2006
Hello,
I have an assignment due very soon. The professor wanted use to shuffle the elements in the array { 3, 5, 1, 7, 0, 9, 2, 6, 4, 8} two times and print the results. He stated the method printArray () would print out the arrray in a nicely formatted fastion such that there would be two blank spaces between adjacent numbers and no more than five numbers to a line. However, my output is single digits from the array listed all vertically? Can you help? I will dance at your wedding, thank you!

public class Shuffle {
public static void shuffleArray ( int [] anArray){
int n = anArray.length;
for (int i = 0; i < n; i++){
int r = i + (int) (Math.random () * (n-i));
int swap = anArray[r];
anArray[r] = anArray ;
anArray[i] = swap;
}
}
public static void printArray(int [] anArray) {
int n = anArray.length;
for (int i = 0; i < n; i++){
System.out.println (anArray [i]);
}
}
public static void main (int [] a) {
int [] anArray = { 3, 5, 1, 7, 0, 9, 2, 6, 4, 8};
int n = anArray.length;
for (int i = 0 ; i < n; i++){
printArray(anArray);
shuffleArray (anArray);
printArray( anArray );
shuffleArray ( anArray );
printArray ( anArray );
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 14 2006
Added on Jul 16 2006
3 comments
114 views