Advertisement
Sketchware

Criar 4 lotofacil verifica números repetidos nas 4 fica vermelho e fala quantos repetidos

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