fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. int[] indexValue = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42};
  12. int count = (int) (Math.random() * 42);
  13. System.out.println("取出数字个数:" + count);
  14. int[] resultIndex = new int[count];
  15. for (int i = 0; i < count; i++) {
  16. int index = (int) (Math.random() * 42);
  17. if (!containValue(resultIndex, index)) {
  18. resultIndex[i] = index;
  19. } else {
  20. i--;
  21. }
  22. }
  23. StringBuffer buffer = new StringBuffer("");
  24. for (int i = 0; i < resultIndex.length; i++) {
  25. int i1 = indexValue[resultIndex[i]];
  26. String strResult = String.valueOf(i1);
  27.  
  28. buffer.append(strResult);
  29. buffer.append(",");
  30.  
  31. //else
  32. //buffer.delete(buffer.lastIndexOf(","),buffer.length());
  33.  
  34. }
  35. if (',' == buffer.charAt(buffer.length() - 1))
  36. buffer = buffer.deleteCharAt(buffer.length() - 1);
  37. System.out.print(buffer);
  38. }
  39. private static boolean containValue(int[] resultIndex, int index){
  40. for (int j = 0; j < resultIndex.length; j++){
  41. if (index == resultIndex[j]){
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. }
Success #stdin #stdout 0.09s 27808KB
stdin
Standard input is empty
stdout
取出数字个数:25
34,39,5,9,7,35,11,19,32,41,27,6,33,17,42,25,37,3,26,22,21,13,18,8,24