From 317d96755c7be3625f65762ea73c31a83a79d6d2 Mon Sep 17 00:00:00 2001 From: Jordi Ayala Date: Mon, 11 Dec 2023 13:26:30 -0600 Subject: [PATCH] Reto #47 - Java --- .../java/asjordi.java" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [F\303\241cil]/java/asjordi.java" diff --git "a/Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [F\303\241cil]/java/asjordi.java" "b/Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [F\303\241cil]/java/asjordi.java" new file mode 100644 index 0000000000..eb84855a4d --- /dev/null +++ "b/Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [F\303\241cil]/java/asjordi.java" @@ -0,0 +1,48 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class Words { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Words w = new Words(); + int wordValue = 0; + String word = ""; + System.out.println("Enter a word, the program ends if the word equals 100 points."); + + while (wordValue != 100) { + word = sc.nextLine(); + wordValue = w.calculate(word); + System.out.println("Value = " + wordValue); + } + + System.out.println("Congratulations! The word " + word + " is equal to 100"); + } + + private final Map map; + + public Words() { + this.map = new HashMap<>(); + loadCharacters(); + } + + public int calculate(String word){ + int sum = 0; + + for (char c : word.toLowerCase().toCharArray()) { + if (this.map.containsKey(c)) sum += this.map.get(c); + } + + return sum; + } + + private void loadCharacters(){ + char[] alphabet = "abcdefghijklmnñopqrstuvwxyz".toCharArray(); + + for (int i = 0; i < alphabet.length; i++) { + this.map.put(alphabet[i], i + 1); + } + } + +}