-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6059 from ASJordi/main
Reto #47 - Java
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [Fácil]/java/asjordi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |