Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ejercicio;
- import java.util.HashMap;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class JuegoTetris {
- static HashMap<String, Integer> mapaPuntuaciones;
- static Scanner sc;
- public static void main(String[] args) {
- mapaPuntuaciones = new HashMap<String, Integer>();
- sc = new Scanner(System.in);
- int opcion = 1;
- boolean continuar = true;
- while(continuar) {
- opcion = solicitarOpcion();
- switch (opcion) {
- case 2:
- mostrarPuntuaciones();
- break;
- case 1:
- registrarPuntuaciones();
- break;
- default:
- continuar = false;
- break;
- }
- }
- System.out.println("FIN DEL PROGRAMA");
- sc.close();
- }
- private static void registrarPuntuaciones() {
- System.out.println("\nIntroduce el nick de usuario");
- String nick = sc.nextLine();
- System.out.println("Introduce la puntuacion: ");
- int punt = Integer.parseInt(sc.nextLine());
- if (mapaPuntuaciones.containsKey(nick)) {
- if (mapaPuntuaciones.get(nick) >= punt) {
- System.out.println("¡PUNTUACION NO SUPERADA!");
- } else {
- mapaPuntuaciones.put(nick, punt);
- System.out.println("¡NUEVO RECORD!");
- }
- } else {
- mapaPuntuaciones.put(nick, punt);
- System.out.println("¡PUNTUACION ALMACENADA!");
- }
- }
- private static void mostrarPuntuaciones() {
- if (mapaPuntuaciones.isEmpty()) {
- System.out.println("\nNo hay ninguna puntuacion almacenda");
- } else {
- TreeMap<String, Integer> treePuntuaciones = new TreeMap<String, Integer>();
- treePuntuaciones.putAll(mapaPuntuaciones);
- System.out.println("\nListado puntuaciones");
- for (Entry<String, Integer> elemento : treePuntuaciones.entrySet()) {
- System.out.println(elemento.getKey() + " - " + elemento.getValue() + " PUNTOS");
- }
- }
- }
- private static int solicitarOpcion() {
- System.out.println("Introduce:");
- System.out.println("2 - para mostrar las puntuaciones\r\n" +
- "\n1 - para registrar un nuevo resultado\r\n" +
- "\n0 - para salir del programa\r\n");
- int opcion = Integer.parseInt(sc.nextLine());
- return opcion;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement