Jawa / Говнокод #28791 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
public class MathGame {

    public static Map<Integer, String> data = new HashMap<Integer, String>();
    public static Map<String, Integer> result = new HashMap<String, Integer>();
    String problem;
    Random random = new Random();

    public void NewProblem() {

        String sign = null;

        for (int i = 1; i < 5; i++) {

            switch (GenNumber(3)) {
                case 1:
                    sign = Sign.MINUS.get();
                    break;
                case 2:
                    sign = Sign.PLUS.get();
                    break;
                case 3:
                    sign = Sign.TIMES.get();
                    break;
                default:
                    sign = Sign.PLUS.get();
                    break;
            }

            problem = GenNumber(9) + " " + sign + " " + GenNumber(9);
            result.put(problem + " = ???", Decide(Integer.parseInt(
                    problem.split(" ")[0]), problem.split(" ")[1], Integer
                    .parseInt(problem.split(" ")[2])));
            data.put(i, problem);

        }
    }

    private int GenNumber(int s) {
        return random.nextInt(s) + 1;
    }

    private int Decide(int a, String sign, int b) {
        switch (sign) {
            case "+":
                return a + b;
            case "-":
                return a - b;
            case "*":
                return a * b;
        }
        return 0;
    }

    public enum Sign {
        PLUS("+"),
        MINUS("-"),
        TIMES("*");

        private final String sign;

        Sign(String sign) {
            this.sign = sign;
        }

        public String get() {
            return sign;
        }

    }

}

Запостил: girixok149 girixok149, (Updated )

Комментарии (1) RSS

  • public class MathGame {
    
      private static final Map<String, Integer> result = new LinkedHashMap<>();
      private final Random random = new Random();
    
      public void runMathProblems(int times) {
    
        for (int i = 0; i < times; i++) {
          int a = randomSingleDigitInt();
          int b = randomSingleDigitInt();
          Sign sign = randomSign();
    
          String problem = a + " " + sign.getSymbol() + " " + b;
    
          if (result.containsKey(problem)) {
            continue;
          }
    
          result.put(problem, calculate(a, sign, b));
        }
      }
    
      public static Map<String, Integer> getResultCopy() {
        return Collections.unmodifiableMap(new LinkedHashMap<>(result));
      }
    
      private int randomSingleDigitInt() {
        return random.nextInt(10);
      }
    
      private Sign randomSign() {
        Sign[] signs = Sign.values();
        return signs[random.nextInt(signs.length)];
      }
    
      private int calculate(int a, Sign sign, int b) {
        switch (sign) {
          case PLUS:
            return a + b;
          case MINUS:
            return a - b;
          case TIMES:
            return a * b;
        }
        return 0;
      }
    
    }
    
    public enum Sign {
      PLUS("+"),
      MINUS("-"),
      TIMES("*");
    
      private final String symbol;
    
      Sign(String symbol) {
        this.symbol = symbol;
      }
    
      public String getSymbol() {
        return symbol;
      }
    
    }
    
    public class Main {
      public static void main(String[] args) {
        MathGame mathGame = new MathGame();
    
        mathGame.runMathProblems(4);
        printDataAndResult();
    
    //    mathGame.runMathProblems(5);
    //    printDataAndResult();
      }
    
      public static void printDataAndResult() {
        String delimiter = " -> ";
    
        System.out.println("data:");
        int i = 1;
        for (String problem: MathGame.getResultCopy().keySet()) {
          System.out.println(i + delimiter + problem);
          i++;
        }
    
        System.out.println("\nresult:");
        MathGame.getResultCopy().forEach((key, value) -> System.out.println(key + " = ???" + delimiter + value));
      }
    
    }
    Ответить

Добавить комментарий

Я, guest, находясь в здравом уме и твердой памяти, торжественно заявляю:

    А не использовать ли нам bbcode?


    8