Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TextAndRegexExercise;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class NetherRealms_13 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, List<Double>> demons = new TreeMap<>();
- String[] input = scanner.nextLine().split("\\s+,\\s+");
- for (int i = 0; i < input.length; i++) {
- String healthString = "";
- double health = 0;
- double damage = 0;
- String operations = "";
- Pattern pHealth = Pattern.compile("([^\\.\\/\\-+\\*0-9]*)");
- Matcher pMatch = pHealth.matcher(input[i]);
- demons.put(input[i], new ArrayList<>());
- while (pMatch.find()) {
- healthString += pMatch.group();
- }
- for (int j = 0; j < healthString.length(); j++) {
- health += healthString.charAt(j);
- }
- demons.get(input[i]).add(health);
- Pattern pDamage = Pattern.compile("[-+]?\\d+(\\.(\\d+))?");
- Matcher dMatch = pDamage.matcher(input[i]);
- while (dMatch.find()) {
- double x = Double.parseDouble(dMatch.group());
- damage += x;
- }
- for (int j = 0; j < input[i].length(); j++) {
- if (input[i].charAt(j) == '/' || (input[i].charAt(j) == '*')) {
- operations += (input[i].charAt(j));
- }
- }
- for (int j = 0; j < operations.length(); j++) {
- if (operations.charAt(j) == '/') {
- damage /= 2;
- } else if (operations.charAt(j) == '*') {
- damage *= 2;
- }
- }
- demons.get(input[i]).add(damage);
- }
- demons.forEach((key,value) -> {
- System.out.println(String.format("%s - %.0f health, %.2f damage", key,value.get(0),value.get(1)));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement