Java IF statements not working properly -
there problem if statements. error shows problem me figure out what's wrong ? main task motor insurance company has 4 categories of insurance based on age , gender of applicant.
this code :
string gender, age; char group; int genderint, ageint; gender = joptionpane.showinputdialog("please specify gender(1 male, 0 female)"); age = joptionpane.showinputdialog("please enter age"); genderint = integer.parseint(gender); ageint = integer.parseint(age); if (gender = 0 || 1 && age = > 18 && < 26) { group = "category a"; } else if (gender = 0 && age = > 27 && < 60) { group = "category b"; } else if (gender = 1 && age = > 27 && < 60) { group = "category c"; } else if (gender = 0 || 1 && age = > 60) { group = "category d"; } else if (gender = 0 || 1 && age = < 18) { joptionpane.showmessagedialog(null, "sorry, you're young"); } joptionpane.showmessagedialog(null, "you have been assignet to" + group); } }
try this,
- use
==
comparison - no space between
<=
,>=
- group
or
conditions in parenthesis - use separate variable each comparison
- use
<=
not=<
, use>=
instead of=>
use
genderint
,ageint
variables int comparisonif ((genderint == 0 || genderint ==1) && (ageint >=18 && ageint < 26)) { group = "category a"; } else if(genderint == 0 && ageint >= 27 && ageint < 60){ group = "category b"; } else if(genderint == 1 && ageint >= 27 && ageint < 60) { group = "category c"; } else if((genderint == 0 || genderint == 1) && ageint >= 60) { group = "category d"; } else if((genderint == 0 || genderint ==1) && ageint <18){ joptionpane.showmessagedialog(null,"sorry, you're young"); }
Comments
Post a Comment