[Solved] my code don’t work in Main class [duplicate]


You use post increment and decrement operator :

    return number++;
    return number--;

For example here :

public static int addOne(int number) {
    return ++number;
}

The value number is incremented but only as soon as the next statement.
So as you return, the same value as the parameter is returned.

It is the same thing here :

public static int drawOne(int number) {
    return number--;
}

number will never be decremented as the number is returned in the same statement that the decrementation.

You have several workarounds :

Make things in 2 times :

public static int addOne(int number) {
    number++;  // post increment
    return number; // value incremented here
}

Use pre increment operator :

public static int addOne(int number) {
    return ++number; // incremented right now
}

Or more simply increment with 1 :

public static int addOne(int number) {
    return number+1; // incremented right now
}

It is exactly the same logic for the decrement operator.

0

solved my code don’t work in Main class [duplicate]