Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. package ejercicio;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map.Entry;
  5. import java.util.Scanner;
  6. import java.util.TreeMap;
  7.  
  8. public class JuegoTetris {
  9.  
  10. static HashMap<String, Integer> mapaPuntuaciones;
  11. static Scanner sc;
  12. public static void main(String[] args) {
  13. mapaPuntuaciones = new HashMap<String, Integer>();
  14. sc = new Scanner(System.in);
  15. int opcion = 1;
  16. boolean continuar = true;
  17.  
  18. while(continuar) {
  19. opcion = solicitarOpcion();
  20.  
  21. switch (opcion) {
  22. case 2:
  23. mostrarPuntuaciones();
  24. break;
  25.  
  26. case 1:
  27. registrarPuntuaciones();
  28. break;
  29. default:
  30. continuar = false;
  31. break;
  32. }
  33. }
  34. System.out.println("FIN DEL PROGRAMA");
  35. sc.close();
  36. }
  37.  
  38. private static void registrarPuntuaciones() {
  39. System.out.println("\nIntroduce el nick de usuario");
  40. String nick = sc.nextLine();
  41.  
  42. System.out.println("Introduce la puntuacion: ");
  43. int punt = Integer.parseInt(sc.nextLine());
  44.  
  45. if (mapaPuntuaciones.containsKey(nick)) {
  46. if (mapaPuntuaciones.get(nick) >= punt) {
  47. System.out.println("¡PUNTUACION NO SUPERADA!");
  48. } else {
  49. mapaPuntuaciones.put(nick, punt);
  50. System.out.println("¡NUEVO RECORD!");
  51. }
  52.  
  53. } else {
  54. mapaPuntuaciones.put(nick, punt);
  55. System.out.println("¡PUNTUACION ALMACENADA!");
  56. }
  57. }
  58.  
  59. private static void mostrarPuntuaciones() {
  60. if (mapaPuntuaciones.isEmpty()) {
  61. System.out.println("\nNo hay ninguna puntuacion almacenda");
  62. } else {
  63.  
  64. TreeMap<String, Integer> treePuntuaciones = new TreeMap<String, Integer>();
  65. treePuntuaciones.putAll(mapaPuntuaciones);
  66.  
  67. System.out.println("\nListado puntuaciones");
  68. for (Entry<String, Integer> elemento : treePuntuaciones.entrySet()) {
  69. System.out.println(elemento.getKey() + " - " + elemento.getValue() + " PUNTOS");
  70. }
  71. }
  72. }
  73.  
  74. private static int solicitarOpcion() {
  75. System.out.println("Introduce:");
  76. System.out.println("2 - para mostrar las puntuaciones\r\n" +
  77. "\n1 - para registrar un nuevo resultado\r\n" +
  78. "\n0 - para salir del programa\r\n");
  79. int opcion = Integer.parseInt(sc.nextLine());
  80. return opcion;
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement