fork(3) 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. for (int i = 0; i < resultIndex.length; i++) {
  24. int i1 = indexValue[resultIndex[i]];
  25. String strResult = String.valueOf(i1);
  26. StringBuffer buffer = new StringBuffer(strResult);
  27.  
  28. buffer.append(",");
  29. if (',' == buffer.charAt(buffer.length() - 1))
  30. buffer = buffer.deleteCharAt(buffer.length() - 1);
  31. else
  32. buffer.delete(buffer.lastIndexOf(","),buffer.length());
  33. System.out.print(buffer);
  34. }
  35. }
  36. private static boolean containValue(int[] resultIndex, int index){
  37. for (int j = 0; j < resultIndex.length; j++){
  38. if (index == resultIndex[j]){
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. }
Success #stdin #stdout 0.09s 27772KB
stdin
Standard input is empty
stdout
取出数字个数:38
395163126362812932423740204112914273518372224381917262341213013251534