Advertisement
Sketchware

TEXTVIEW1 TEXTVIE2 GERA 50 NÚMEROS DE 0 A 100 SEM REPETIR

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