java - Implementation of switch case for check box -
is there way implement switch case checkbox?
example: have 4 checkboxes, if selected 2 checkboxes, how trigger case output want?
double exam = 0.0, assign = 0.0, quiz = 0.0, ct = 0.0; if (examchkbox.isselected()) { exam = double.parsedouble(examtextfield.gettext()); } if(ctchkbox.isselected()) { ct = double.parsedouble(cttextfield.gettext()); } if(quizchkbox.isselected()) { quiz = double.parsedouble(quiztextfield.gettext()); } if(asschkbox.isselected()) { assign = double.parsedouble(asstextfield.gettext()); } if (!(exam + ct + quiz + assign == 100)) { markerrorlbl.settext("total marks must 100"); } else { // implementation of code here }
design view.
let ticked exam , class test, want select value in text field , store marks using switch case. possible?
this thing want i've no idea how implement checkbox.
switch(x) { case 1 : = new exam(marks) ;total+=marks; break; case 2 : = new test(marks) ;total+=marks; break; case 3 : = new quiz(marks) ;total+=marks; break; case 4 : = new assignment(marks) ;total+=marks;break; }
why want use switch instead of if statements have in code?
since can select 4 checkboxes independently each other, switch
not best solution here. have 4 checkboxes, there 2^4 = 16 possible "check patterns":
int pattern = (cb1.isselected() ? 0b0001 : 0) | (cb2.isselected() ? 0b0010 : 0) | (cb3.isselected() ? 0b0100 : 0) | (cb4.isselected() ? 0b1000 : 0); switch (pattern) { case 0b0001: // code when checkbox 1 checked break; ... case 0b0011: // code when checkbox 1 , checkbox 2 checked break; ... case 0b1011: // code when checkbox 1, 2 , 4 checked break; ... }
if use switch, need 16 cases. in contrast, need 4 if statements:
if (checkbox 1 checked) { // code when checkbox 1 checked } if (checkbox 2 checked) { // code when checkbox 2 checked } ...
note these 4 independent if statements, no else if
s there.
Comments
Post a Comment