Advertisement
Guest User

Untitled

a guest
May 28th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5. import java.util.concurrent.ThreadLocalRandom;
  6.  
  7. /**
  8. * Created by Sergi.
  9. * UPV 2016
  10. */
  11. public class probabilidad {
  12. public static void main(String[] args) throws FileNotFoundException{
  13. HashMap<Integer, Integer> alumnos = new HashMap<>();
  14. int repeticiones = 1000;
  15. int totalAlumnos = 20;
  16.  
  17. for (int i = 1; i <= totalAlumnos; i++) {
  18. alumnos.put(i, 0);
  19. }
  20.  
  21. for (int r = 1; r <= repeticiones; r++) {
  22. int random = ThreadLocalRandom.current().nextInt(1, totalAlumnos + 1);
  23. int value = alumnos.get(random);
  24. alumnos.put(random, value + 1);
  25. }
  26.  
  27. Scanner sc = new Scanner(new File("alumnos.txt"));
  28. for (Integer name: alumnos.keySet()){
  29. String key = name.toString();
  30. int value = alumnos.get(name);
  31. float total = (float)value/repeticiones*100;
  32. System.out.printf("%2s %-32s %s \t %2.02f\n",key, sc.nextLine(), value, total);
  33. }
  34. sc.close();
  35.  
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement