Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Random;
  4.  
  5. public class Zadanie3 {
  6.     private final static int X = 100;
  7.     private final static int Y = 1000;
  8.     public static void main(String[] args) {
  9.  //tworze mape(kolekcja klucz,wartosc)
  10.         Map<Integer,Integer> hm = new HashMap<>();
  11. //petle wylosuje Y liczb od 1 do 100(zakres)
  12.         for (int i = 0; i<Y;i++)
  13.         {
  14.             //losuje kolejna liczbe od 1 do 100
  15.             int liczba = random(X);
  16. //sprawdzam czy mapa zawiera klucz, jesli nie zawiera to dodaje klucz i ustala mu wartość zerową
  17.             if (!hm.containsKey(liczba)){
  18.                 hm.put(liczba,0);
  19.             }
  20.             hm.put(liczba,hm.get(liczba) + 1);
  21.  
  22.         }
  23.         for(int liczba: hm.keySet()){
  24.             System.out.println(liczba + "=" + hm.get(liczba));
  25.         }
  26.  
  27.     }
  28.  
  29.     private static int random(int x) {
  30.         Random r = new Random();
  31.         return r.nextInt(x) + 1;
  32.     }
  33.  
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement