Advertisement
Sketchware

Criar 3 cartelas 2 não repete e comparar 1 com a 3 e mostra quantos repetem

Jan 17th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 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. Set<Integer> uniqueNumbers3 = new HashSet<>(); while (uniqueNumbers3.size() < 50) { uniqueNumbers3.add((int)(Math.random()*101)); } List<Integer> randomNumbers4 = new ArrayList<>(uniqueNumbers3);
  40.  
  41. TextView textView4 = (TextView) findViewById(R.id.textview4); textView4.setText(TextUtils.join(", ", randomNumbers4));
  42.  
  43. List<Integer> randomNumbers5 = new ArrayList<>(uniqueNumbers1);
  44. List<Integer> randomNumbers6 = new ArrayList<>(uniqueNumbers3);
  45. List<Integer> repeatedNumbers = new ArrayList<>(randomNumbers5);
  46. repeatedNumbers.retainAll(randomNumbers6);
  47.  
  48. TextView textView5 = (TextView) findViewById(R.id.textview5);
  49. String repeatedNumberText = "Números repetidos : " + repeatedNumbers.size();
  50. textView5.setText(repeatedNumberText);
  51.  
  52.  
  53.  
  54. String repeatedNumber = "Números repetidos: " + randomNumbers3.size();
  55. textView3.setText(repeatedNumber);
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement