Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Array;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Exercise_Zad_5_NR {
- public static void main(String[] args) {
- Scanner scan = new Scanner (System.in);
- String input = scan.nextLine();
- String[] myDeamons = input.split(",\\s+");
- Map<String, List<Double>> myMap = new TreeMap<>();
- for (int i = 0; i < myDeamons.length; i++) {
- String currDeamon = myDeamons[i].replaceAll("\\s+", ""); // Tova go vidqh vav Foruma !!!
- String regexHealth = "[^0-9+\\-*\\/\\.]"; // "[^\\d.+/*,-]*"; // [^\\d+\\-*\\/.]+ ???
- Pattern myPat = Pattern.compile(regexHealth);
- Matcher myMatch = myPat.matcher(currDeamon);
- String currHealtString = ""; // [^0-9+\-*\/\.]
- double currHealtToPut = 0;
- while (myMatch.find()){
- currHealtString += myMatch.group();
- }
- for (int k = 0; k < currHealtString.length(); k++) {
- currHealtToPut += currHealtString.charAt(k);
- }
- // [-+]?\\d\\.?\\d* ???
- String regexDamage = "([-+]?[0-9]*\\.[0-9]+|[0-9]+)|([-+]?[0-9]+)"; // "([+-]?[\\d]+[.]?[\\d]*)";
- Pattern myPattern2 = Pattern.compile(regexDamage);
- Matcher myMatcher2 = myPattern2.matcher(currDeamon); // [-+]?[0-9]*\.[0-9]+|[0-9]+)|([-+]?[0-9]+
- double currDamage = 0;
- while (myMatcher2.find()){
- currDamage += Double.parseDouble(myMatcher2.group());
- }
- for (int j = 0; j < currDeamon.length(); j++) {
- char temp = currDeamon.charAt(j);
- if (temp == '/')
- currDamage /= 2;
- else if (temp == '*')
- currDamage *= 2;
- }
- myMap.putIfAbsent(currDeamon, new ArrayList<>(Arrays.asList(0.0 , 0.0)));
- double oldHealt = myMap.get(currDeamon).get(0);
- myMap.get(currDeamon).set(0, oldHealt + currHealtToPut);
- double oldDamage = myMap.get(currDeamon).get(1);
- myMap.get(currDeamon).set(1, oldDamage + currDamage);
- }
- for (Map.Entry<String, List<Double>> entry : myMap.entrySet()) {
- System.out.printf("%s - %.0f health, %.2f damage%n",entry.getKey(), entry.getValue().get(0), entry.getValue().get(1) );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement