Advertisement
Sketchware

Criar 4 cartelas lotomania e nas 2 primeiras números não repete

Jan 18th, 2023
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. List<Set<Integer>> uniqueNumbersList = new ArrayList<>();
  2. List<List<Integer>> randomNumbersList = new ArrayList<>();
  3. List<TextView> textViewList = new ArrayList<>();
  4.  
  5. for (int i = 0; i < 4; i++) {
  6.     Set<Integer> uniqueNumbers = new HashSet<>();
  7.     while (uniqueNumbers.size() < 50) {
  8.         int num = (int)(Math.random()*100) + 1;
  9.         if(i == 0 || !uniqueNumbersList.get(0).contains(num)) {
  10.             uniqueNumbers.add(num);
  11.         }
  12.     }
  13.     uniqueNumbersList.add(uniqueNumbers);
  14.     List<Integer> randomNumbers = new ArrayList<>(uniqueNumbers);
  15.     randomNumbersList.add(randomNumbers);
  16.     TextView textView = (TextView) findViewById(getResources().getIdentifier("textview" + (i + 1), "id", getPackageName()));
  17.     textView.setText(TextUtils.join(", ", randomNumbers));
  18.     textViewList.add(textView);
  19. }
  20.  
  21. Set<Integer> repeatedNumbers = new HashSet<>(randomNumbersList.get(0));
  22. for (int i = 1; i < 4; i++) {
  23.     repeatedNumbers.retainAll(randomNumbersList.get(i));
  24. }
  25.  
  26. for (Integer num : repeatedNumbers) {
  27.     for (int i = 0; i < 4; i++) {
  28.         int start = textViewList.get(i).getText().toString().indexOf(num.toString());
  29.         int end = start + num.toString().length();
  30.         Spannable spannable = new SpannableString(textViewList.get(i).getText());
  31.         spannable.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  32.         textViewList.get(i).setText(spannable);
  33.     }
  34. }
  35.  
  36. TextView textView5 = (TextView) findViewById(R.id.textview5);
  37. String repeatedNumber = "Números repetidos: " + repeatedNumbers.size();
  38. textView5.setText(repeatedNumber);
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement