Advertisement
Sketchware

Gera 15 números aleatórios de 1 a 25

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