java - How to use a value from the if statement? -


i trying use value created inside "if statement" , print outside of create pop not recognize , not know why. can me please??

@override public void onclick (view v){     int sum = uno + dos + tres + cuatro;     double pru = sum*8;     if(sum == 11){         double tot = pru - (pru*.01);         string tot1 = string.valueof(tot);     }     if(sum == 12){         double tot = pru - (pru*.02);         string tot1 = string.valueof(tot);     }     if(sum == 13){         double tot = pru - (pru*.03);         string tot1 = string.valueof(tot);     }     if(sum == 14){         double tot = pru - (pru*.04);         string tot1 = string.valueof(tot);     }     if(sum == 15){         double tot = pru - (pru*.05);         string tot1 = string.valueof(tot);     }     if(sum == 16){         double tot = pru - (pru*.06);         string tot1 = string.valueof(tot);     }     if(sum == 17){         double tot = pru - (pru*.07);         string tot1 = string.valueof(tot);     }     if(sum == 18){         double tot = pru - (pru*.08);         string tot1 = string.valueof(tot);     }     if(sum == 19){         double tot = pru - (pru*.09);         string tot1 = string.valueof(tot);     }     if(sum >= 20){         double tot;         tot = pru - (pru*.1);         string tot1 = string.valueof(tot);     }  print tot1; 

thanks in advance!

i suggest read this: https://en.wikipedia.org/wiki/scope_%28computer_science%29

the short answer can't you're trying do. variable born in if block vanishes when exit if block. known variable "going out of scope".

to solve issue, declare variable outside if block. won't reproduce entire code, this:

int sum = uno + dos + tres + cuatro; double pru = sum*8; string tot1 = ""; if(sum == 11){     double tot = pru - (pru*.01);     tot1 = string.valueof(tot); } print tot1; 

just of if statements.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -