Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- public class Zadanie3 {
- private final static int X = 100;
- private final static int Y = 1000;
- public static void main(String[] args) {
- //tworze mape(kolekcja klucz,wartosc)
- Map<Integer,Integer> hm = new HashMap<>();
- //petle wylosuje Y liczb od 1 do 100(zakres)
- for (int i = 0; i<Y;i++)
- {
- //losuje kolejna liczbe od 1 do 100
- int liczba = random(X);
- //sprawdzam czy mapa zawiera klucz, jesli nie zawiera to dodaje klucz i ustala mu wartość zerową
- if (!hm.containsKey(liczba)){
- hm.put(liczba,0);
- }
- hm.put(liczba,hm.get(liczba) + 1);
- }
- for(int liczba: hm.keySet()){
- System.out.println(liczba + "=" + hm.get(liczba));
- }
- }
- private static int random(int x) {
- Random r = new Random();
- return r.nextInt(x) + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement