Skip to content

Commit

Permalink
Merge pull request #6059 from ASJordi/main
Browse files Browse the repository at this point in the history
Reto #47 - Java
  • Loading branch information
kontroldev authored Dec 12, 2023
2 parents 47417e8 + 317d967 commit 6513df3
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Character, Integer> 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);
}
}

}

0 comments on commit 6513df3

Please sign in to comment.