how to write an if statement which evaluates 3 dialogue boxes
Hi all
Im trying to write a code that has a for loop which iterates through 3 instance variables, the 3 instance variables are each set using a confirm dialogue box so if the user clicks yes they ae set with the value true and if the user clicks no they are set wit the value false. I have done this and it works fine,
however the next step is to display an alert message if less than 2 of the dialogue boxes evaluate to true. I am extremely stuck on this and ive been working on it for hours now. im thinking I will use an if statement but I dont know how to evaluate the 3 instance variables. I know how to work with one but im not even sure if its possible to evaluate 3 and have the condition evaluate to true if less tan 2 of theinstance variables evaluate to true?
I hope someone can help as im really stuck on this now. :-L
just to clarify, what im trying to do is write a loop following these guidelines:
if less than 2 of the dialogue boxes result in true
do something
or it may be possible to do it like this??:
if less than 2 of the instance variables are equal to true
do something
this doesent have to be a loop if there is another way to do it, i just think a loop is the way I need to do it, however as i am a noob to programming I could ofcourse be wrong!
sorry for not adding code but I am currently moving house so have no internet connection for my pc. I am posting these messages via a mobile phone.
Thanks in advance to anyone who attempts to help me
Re: how to write an if statement which evaluates 3 dialogue boxes
You can count how many are checked....quite a few ways to do this. Here's two methods (there are more than this though):
Using a loop
Code :
int x = 0;
for ( int i = 0; i < 3; i++ ){
if ( value[i] ){ x++; }
}
if ( x < 2 ){
//do something
}else if ( x == 2 ){
//2 are checked, do something else
}else{
//all are checked, do something else
}
In a single statement:
Code :
if ( ( value[0] && value[1] && !value[2] ) ||
( value[0] && !value[1] && value[2] ) ||
( !value[0] && value[1] && value[2] ) ){
//only 2 are checked
}
All code assuming value is an array containing your booleans
Re: how to write an if statement which evaluates 3 dialogue boxes
hi copeg, thankyou for the reply, i worked this out in the end last night. I did it the same as your second example & ive tested it & it all works. thankyou again for the assistance
Re: how to write an if statement which evaluates 3 dialogue boxes
Could you also do this without the array. for exemple by just checking
Code :
if ( x==1)
{
//do something
}
etc etc??
also,
how then would you get the dialog boxes to ask a question (to get the true/false)?? where would you store these true false boolean answers?
Am i making sense?
Re: how to write an if statement which evaluates 3 dialogue boxes
Yes, it can be done without arrays, but you can't compare booleans with integers in Java.
Code :
boolean val1 = true;
if (val1 == 1) // Syntax error
However, you will still need 3 variables to put the values in. Copeg chose an array because that's one of the easiest ways to store a list of variables.
There are two main ways to get a dialog box: make it yourself, or use the JOptionPane class.
Code :
JOptionPane.showConfirmDialog(null,"Do you confirm?");
Note that showConfirmDialog actually returns an integer value, so you have to compare it to the static response values stored in the JOptionPane class:
Code :
if (JOptionPane.showConfirmDialog(null,"Do you confirm") == JOptionPane.YESOPTION)
{
// yes selected
}
Re: how to write an if statement which evaluates 3 dialogue boxes
Ah right I'm with you!!
The only thing is, in the course that I'm doing, we haven't yet learnt about arrays so will not be able to use them.
If I put the answers into 3 variables, how would I then check to see if 2 or more of the variables do contain a 'true'?
could I do something that checks "do you want yes or no" - then if yes is chosen it adds 1 to a value. This would be repeated 3 times and then if the value is 2 or more then something is executed?
I'm sure I'll figure this out but some input would help.
thanks so much (in advance).
John
Re: how to write an if statement which evaluates 3 dialogue boxes
No worries - I've just cracked it!!
Basically I craeted another varaiable and each time added to to it. All's good
Thanks