开发者

Unknown while loop usage

This page a user must choose between one of 2 checkboxes 5 times. So I wrote this:

        if (box1a.isSelected() == true || box1开发者_如何学编程b.isSelected() == true) {
            if (box2a.isSelected() == true || box2b.isSelected() == true) {
                if (box3a.isSelected() == true || box3b.isSelected() == true) {
                    if (box4a.isSelected() == true || box4b.isSelected() == true) {
                        if (box5a.isSelected() == true || box5b.isSelected() == true) {


                                 with some other things he does when it is true.


        } else {
            new Error("You must select an answer at all the questions");
        }

Then he only returns a error if you don't check one of the top checkboxes. Then cleary I need a while loop in there but i don't know how to uhm do it. I know how a while loop works but don't know how It would look in this situation. Please help

Also now I have to do the same with text fields and using th same methode that I got answered by you guys doesn't work. any advise?


if ((box1a.isSelected() || box1b.isSelected()) &&
   (box2a.isSelected() || box2b.isSelected())  &&
   (box3a.isSelected() || box3b.isSelected())  &&
   (box4a.isSelected() || box4b.isSelected())  &&
   (box5a.isSelected() || box5b.isSelected())) 
   {
      //true stuff
   }
   else 
   {
       new Error("You must select an answer at all the questions");
   }

You should never shouldn't test for true with ==. It is poor style, better to just use the return value from isSelected()


if ((box1a.isSelected() == true || box1b.isSelected() == true) &&
   (box2a.isSelected() == true || box2b.isSelected() == true) &&
   (box3a.isSelected() == true || box3b.isSelected() == true) &&
   (box4a.isSelected() == true || box4b.isSelected() == true) &&
   (box5a.isSelected() == true || box5b.isSelected() == true)) {
      //DO SOMETHING IF TRUE
}
else {
      new Error("You must select an answer at all the questions");
}

No looping needed ^_^


why don't you use radio button (with a default radio button checked) in this case ?


A general strategy would be something like this:

bool flag = true;
do{
    //search for input
    if (/*the input is valid*/)
        flag = false;
}while (flag);

But if you hard code so many options, you might have the wrong design. Try something like a radio button like Jerome C. suggested.


if(!box1a.isSelected() && !box1b.isSelected()) {
    // You must select an answer at all the questions
}
else if (box1a.isSelected() && box1b.isSelected() && box2a.isSelected() && box2b.isSelected() && box3a.isSelected() && box3b.isSelected() && box4a.isSelected() && box4b.isSelected() && box5a.isSelected() && box5b.isSelected()) {
    // with some other things he does when it is true.
}

A few points to note here.

  1. Avoid using class names like Error as they're normally used for genuine java.lang.Error logic.
  2. If you have a boolean, you don't need to use a == operator.


Not sure why you want a while-loop. If you are thinking that the user must "stay in the loop" while the your condition (all 5 questions answered) is not met, then it is unnecessary. The Event Dispatch Thread (EDT) will continue running the "loop" for you.

On the other hand, if you are looking for a compact way to verify all of your checkboxes, you can change how they are declared from (assuming) javax.swing.JCheckbox box1a; etc. to either a fixed array or an ArrayList which you can then iterate over with a for-loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜