C
C
Charik6352021-06-13 16:32:46
Java
Charik635, 2021-06-13 16:32:46

How to add a "-" sign in front of an expression in ENUM?

enum Roman {
    O(0), I(1), V(5), X(10), L(50), C(100), -I(-1),- V(-5), -X(-10),- L(-50),- C(-100); // здесь если ввести минусы, то компилятор ругается
    static Roman valueOf(int value) {
        for (Roman d : Roman.values())
            if (value == d.getValue())
                return d;
        throw new IllegalArgumentException();
    }

    Roman(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }

    int value;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BorLaze, 2021-06-13
@BorLaze

No way.
The enum value is a normal Java identifier and the dash character is not allowed in it.

O
Orkhan, 2021-06-13
Hasanly @azerphoenix

You can try using underscores instead of dashes.
-V=> _V
You can look at an enum example for Roman numerals here:
https://stackoverflow.com/questions/3921866/how-do...

A
acwartz, 2021-06-15
@acwartz

Well, write a function that will return a value in a negative value (x *= -1).
Type Roman.V.nagative ()
And that as you want - it is impossible. - this is an arithmetic operator and then a sign that cannot be used in the names of variables and classes.
I would not bother with the function, I would start V (5) and negV (-5)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question