vendredi 19 février 2016

Updating the value of a variable from the outer scope within an if block

int i = 3;
int j = 2;
int k = 1;

Printer printer = new Printer();

if (printer.getTotalAmount() > 0) {
   if (printer.getType().equals("canon")) {
      if (i >= j) {
         i = i-j;  // i=3-2, so i will be 1 now
      }
   }
   if (printer.getType().equals("epson")) {
      if (i >= k) {
        i = i - k; // it should be i = 1-1 and i will be 0 now
      }
   }
}

My problem is that the variable i's value is not updated after the previous if statement. Due to block scope, the variable i's value is still 3.

How can I solve this problem?

Aucun commentaire:

Enregistrer un commentaire