[Solved] Java using a method value to subtract a object field value


As the comments have suggested you need to change the return type of the function to return int and you need to return your damageDelt variable. One way to do this is declare it outside of your if statements and then return it at the end of the function. If you didn’t want to do it that way you could add a return statement inside each of your if blocks but that would be a bit messy.
So your function might look like this:

public int railGunAttack() {

    int randomNumber = (int) (Math.random() * 100 + 1);
    int damageDelt = 0;

    if (randomNumber > 0 && randomNumber < 50) {
        damageDelt = 2 * randomNumber;
        System.out.println("Railgun did " + damageDelt + " Damage");
    } else if (randomNumber > 50 && randomNumber < 80) {
        damageDelt = 4 * randomNumber;
        System.out.println("Railgun did " + damageDelt + " Damage");
    } else if (randomNumber > 80 && randomNumber < 100) {
        damageDelt = 50 - randomNumber;
        System.out.println("Railgun did " + damageDelt + " Damage " + "Railgun projectiles glazed the target");
    } else {
        System.out.println("Railgun missed target");
    }
    return damageDelt;
}

0

solved Java using a method value to subtract a object field value