Advertisement
Sketchware

Criar 15 números de 0 a 25 mostra quantos repete e fica vermelho repetidos

Jan 18th, 2023
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. Set<Integer> uniqueNumbers1 = new HashSet<>();
  2. while (uniqueNumbers1.size() < 16) {
  3.     uniqueNumbers1.add((int)(Math.random()*25));
  4. }
  5. List<Integer> randomNumbers1 = new ArrayList<>(uniqueNumbers1);
  6.  
  7. TextView textView1 = (TextView) findViewById(R.id.textview1);
  8. textView1.setText(TextUtils.join(", ", randomNumbers1));
  9.  
  10. Set<Integer> uniqueNumbers2 = new HashSet<>();
  11. while (uniqueNumbers2.size() < 16) {
  12.     uniqueNumbers2.add((int)(Math.random()*25));
  13. }
  14. List<Integer> randomNumbers2 = new ArrayList<>(uniqueNumbers2);
  15.  
  16. TextView textView2 = (TextView) findViewById(R.id.textview2);
  17. textView2.setText(TextUtils.join(", ", randomNumbers2));
  18.  
  19. List<Integer> randomNumbers3 = new ArrayList<>(randomNumbers1);
  20. randomNumbers3.retainAll(randomNumbers2);
  21.  
  22. for (Integer num : randomNumbers3) {
  23.     int start1 = textView1.getText().toString().indexOf(num.toString());
  24.     int end1 = start1 + num.toString().length();
  25.     int start2 = textView2.getText().toString().indexOf(num.toString());
  26.     int end2 = start2 + num.toString().length();
  27.  
  28.     Spannable spannable1 = new SpannableString(textView1.getText());
  29.     spannable1.setSpan(new ForegroundColorSpan(Color.RED), start1, end1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  30.     textView1.setText(spannable1);
  31.     Spannable spannable2 = new SpannableString(textView2.getText());
  32.     spannable2.setSpan(new ForegroundColorSpan(Color.RED), start2, end2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  33.     textView2.setText(spannable2);
  34. }
  35.  
  36. TextView textView3 = (TextView) findViewById(R.id.textview3);
  37. String repeatedNumber = "Números repetidos: " + randomNumbers3.size();
  38. textView3.setText(repeatedNumber);
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement