Results 1 to 5 of 5
- 04-28-2008, 12:32 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
showing results in a for loop randomly
Hello there, I would like to display results from a from loop randomly and I have no idea how to do it. This is the for loop that i have:
I basically want to display some text I have stored in variables else where, but i don't want to display them serially, i want to show them randomly. I also don't want any of the text to be displayed more then once.Java Code:for (int i = 0; i < object.length(); i++) { System.out.println(displaytext(i)); }
All help is appreciated, thanks!Last edited by vexity; 04-28-2008 at 12:33 PM. Reason: forgot to mention something
- 04-28-2008, 12:35 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Where your texts are stored. In an array, if so find the index randomly and get the text.
- 04-28-2008, 04:17 PM #3
Because of your "display once" requirement you need to implement a more complicated solution. You have to implement a shuffle algorithm. For this you can randomly shuffle an array of indexes to your objects and then display the text at each shuffled index. To shuffle the array of indexes you can go once through the array with a for loop and for each index swap the value with another randomly picked location in the array. Sorry I don't have time to write the code right now. Maybe later if you still need it.
Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
- 04-29-2008, 01:32 AM #4
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
I'd appreciate it much if you can still show me the code.
-thanks
- 04-29-2008, 04:24 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, try this code.
What you have tried up to now pal. :)Java Code:import java.util.Arrays; import java.util.Random; /** * * @author Eranga Tennakoon */ public class vexity { private String[] numbers = {"One", "Two", "Three", "Four", "Five", "Six", "Seven"}; private Random random = new Random(); public static void main(String[] args) { new vexity().doSelection(); } private void doSelection() { int[] index = new int[numbers.length]; Arrays.fill(index, -1); for (int i = 0; i < index.length; i++) { int num = getNumber(index); index[i] = num; System.out.println("random value: " + num); System.out.println("word : " + numbers[num]); } } private int getNumber(int[] array) { int n; do { n = random.nextInt(numbers.length); } while(isContain(array, n)); return n; } private boolean isContain(int[] array, int n) { for(int i = 0; i < array.length; i++) { if(array[i] == n) return true; } return false; } }
Similar Threads
-
Problem with displaying search results from an array
By BHCluster in forum New To JavaReplies: 4Last Post: 04-24-2008, 03:34 AM -
date and calender not getting the right results
By valoyivd in forum New To JavaReplies: 4Last Post: 04-14-2008, 11:51 AM -
BigInteger remainder results in zero
By perito in forum New To JavaReplies: 1Last Post: 03-21-2008, 04:07 PM -
results to code disappear too fast for DOS window
By dubdubdub in forum New To JavaReplies: 3Last Post: 12-29-2007, 05:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks