Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. //This code will simulate the lottery draw
  2. import java.util.Scanner;
  3.  
  4. public class Task2 {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. Scanner input = new Scanner(System.in);
  9. System.out.println("Enter from how many numbers we extract: ");
  10. int n = input.nextInt();
  11. System.out.println("Enter how many numbers we extract:");
  12. int k = input.nextInt();
  13. int[] arr = new int[k];
  14. input.close();
  15.  
  16. for(int i = 0; i < k; i++)
  17. {
  18. int random = (int) (Math.random() * n) + 1;
  19.  
  20. for (int j = 0; j < i; j++ )
  21. {
  22. if (arr[j] == random)
  23. {
  24. random = (int) (Math.random() * n) + 1;
  25. j = -1;
  26. }
  27.  
  28. }
  29. arr[i] = random;
  30.  
  31. }
  32. for(int i = 0; i < k; i ++)
  33. System.out.println(arr[i] + " ");
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement