Advertisement
JVFabia

Colecciones - 1-10

Sep 25th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. package org.forge;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Map<Integer, Integer> contador = new HashMap<Integer, Integer>();
  10.         Scanner in = new Scanner(System.in);
  11.         while (true) {
  12.             System.out.println("ingrese numero entre 1 y 10. -1 para salir");
  13.             int x = in.nextInt();
  14.             if (x == -1) break;
  15.             if (x < 1 || x > 10) {
  16.                 System.out.println("número inválido");
  17.                 continue;
  18.             }
  19.             System.out.println("control while");
  20.             if (contador.containsKey(x)) {
  21.                 contador.put(x, contador.get(x) + 1);
  22.             } else {
  23.                 contador.put(x, 1);
  24.             }
  25.         }
  26.         for (int x = 1; x <= 10; x++) {
  27.             System.out.println(x + " : " + contador.getOrDefault(x, 0));
  28.         }
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement