Why java doesn't recognize a variable? -
does know why java doesn't understand variable "increment"? tasked find out user's years of service , salary. after that, assign each of them increment. when want print variable "increment", java doesn't recognize it. may know why?
import javax.swing.joptionpane; public class q4 { public static void main(string[] args) { int increment; string yearsstring = joptionpane.showinputdialog(null, "please enter years of service"); string salarystring = joptionpane.showinputdialog(null, "please enter salary"); int years = integer.parseint(yearsstring); double salary = double.parsedouble(salarystring); if(years < 10) { if(salary < 1000.0) { increment = 100; } else if (salary< 2000.0) { increment = 200; } else { increment = 300; } } else if (years > 10) { if(salary<1000) { increment = 200; } else if (salary < 2000) { increment = 300; } else { increment = 400; } } joptionpane.showmessagedialog(null, "your increment "+increment); } }
the problem not "recognizing" of variable declaration.
cannot use local variable if compiler considers may not valued.
in actual code, increment
may not valued assign value to, in specific conditional statements.
so value increment
in declaration default value :
int increment = 0;
Comments
Post a Comment