vendredi 26 mai 2017

My if-else statements aren't doing the things that are in the second and third "else if"

Ignore the fact that the text is in swedish (if you can).

The problem is that when i am running the program and i press "yes" on the first two questions that pop up, the first if statement works fine, but whenever i press, for example "yes" then "no", nothing happens, and i think it's because this else if statement is after the first if statement, so it's like skipping the ones after the first if statement, would really appreciate some help.

import javax.swing.JOptionPane;

public class Slutuppgift extends javax.swing.JFrame {

public Slutuppgift() {
    initComponents();
}                                       

    Object[] options = {"Osäker",
                "Nej",
                "Ja"};

int svar_1 = JOptionPane.showOptionDialog(null,
"Är bilden tagen i europa? "
,
"Fråga 1",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options, null);



int svar_2 = JOptionPane.showOptionDialog(null,
"Är det vatten med i bilden?"
,
"Fråga 2",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options, null);

 if (svar_1 == 1 && svar_2 == 1){
    int svar_3 = JOptionPane.showOptionDialog(null,
"Är platsen på bilden associerad med hasardspel?"
,
"Fråga 3",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options, null);   


if (svar_3 == 2)
JOptionPane.showMessageDialog(null,"Du tänkte på Vita Huset");
}

else if (svar_3 == 1){
JOptionPane.showMessageDialog(null,"Du tänkte på Las Vegas");
}

else if (svar_3 == 0){
JOptionPane.showMessageDialog(null,"Du tänkte inte på någon bild alls, du klickar bara på osäker");
}

else if (svar_1 == 2 && svar_2 == 1){
int svar_4 = JOptionPane.showOptionDialog(null,
"Är det mycket grönska på bilden?"
,
"Fråga 3",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options, null); 

if (svar_4 == 2){
    int svar_5 = JOptionPane.showOptionDialog(null,
"Finns det moln på bilden?"
,
"Fråga 4",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options, null);

if(svar_5 == 2){
    JOptionPane.showMessageDialog(null,"Du tänkte på Eiffeltornet");
}    
else if (svar_5 == 1){
    JOptionPane.showMessageDialog(null,"Du tänkte på lutande tornet i Pisa");
}
} 


else if (svar_1 == 1 && svar_2 == 2){
    int svar_6 = JOptionPane.showOptionDialog(null,
"Är platsen associerad med musik?"
,
"Fråga 3",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options, null); 
}    

else if (svar_1 == 2 && svar_2 == 2){
    int svar_7 = JOptionPane.showOptionDialog(null,
"Är byggnaden gammal?"
,
"Fråga 3",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options, null); 

        int svar_6 = 0;

if (svar_7 == 2){
    JOptionPane.showMessageDialog(null,"Du tänkte på Big Ben");
}

else if (svar_7 == 1){
    JOptionPane.showMessageDialog(null,"Du tänkte på Turning Torso");
}


}                                        

private void bild1MouseClicked(java.awt.event.MouseEvent evt) {                                   

}                                  

public static void main(String args[]) {

    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://ift.tt/1cNmMj1 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Slutuppgift.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Slutuppgift.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Slutuppgift.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Slutuppgift.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>


    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Slutuppgift().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel bild1;
private javax.swing.JLabel bild2;
private javax.swing.JLabel bild3;
private javax.swing.JLabel bild4;
private javax.swing.JLabel bild5;
private javax.swing.JLabel bild6;
private javax.swing.JLabel bild7;
private javax.swing.JLabel bild8;
private javax.swing.JLabel bild9;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel välj;
// End of variables declaration                   

}

Aucun commentaire:

Enregistrer un commentaire