0

I have a problem here when I write this example in java:

if ( deadline==1){ 
    total = A + B + C;
    System.out.println(" you get =" + total);
}
if ( deadline==2 ){ 
    total = A + B + C;
    Punishment = total*(5\100);
    Final = total - Punishment;
    System.out.println(" you get =" + Final);
}

And when I run it, I write on the black screen the deadline =2
but it shows on the black screen ( you get = total)

To be clear that I have values of ( A , B , C). I write it before if statement + I ask the user before to write the number of the deadline using Scanner.

NoobTW
  • 2,017
  • 2
  • 19
  • 35
beth
  • 5
  • 2
  • 1
    Please provide a complete example demonstrating the problem. We can't just take your word for it that everything you did before the `if` statement must be correct and that `if` itself is broken in Java. – David Apr 19 '21 at 17:35
  • see, for example [Int division: Why is the result of 1/3 == 0?](https://stackoverflow.com/q/4685450/153588480) –  Apr 19 '21 at 17:44

1 Answers1

1

In Java, if both operands of an operation are integers, the operation is done as an integer operation.

Your code is doing Punishment = total*(5\100) - here the division is done as an integer operation, that is, the result of (5*100) is 0 (zero) and so Punishment will be zero too.

Option 1: change it to Punishment = (total * 5) \ 100 so the multiplication is done first (parenthesis not needed).

Option 2: use double like Punishment = (int)(total * (5.0\100))

Option 3: also change the variable to double:

double punishment;
...
punishment = total * 5.0 / 100.0;

Note: variable names should start with lowercase letter by convention

assuming that deadline is 2