Results 1 to 18 of 18
Thread: break statement
- 06-01-2010, 09:37 PM #1
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
break statement
hello
does the break statement works with if and else ?
because i`m trying to use it but there is some error that says the break should be out the switch or loop
PHP Code:if (jRadioButton1.isSelected()) { jLabel2.setText(null); jLabel1.setText("how old am I"); jRadioButton1.setText("15"); jRadioButton2.setText("16"); jRadioButton3.setText("17"); jRadioButton4.setText("18"); buttonGroup1.clearSelection(); break; } else jLabel2.setText("wrong");
- 06-01-2010, 10:00 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Indeed, break should only be used in a loop or switch. It cannot be used in if/else.
- 06-01-2010, 10:08 PM #3
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
so how it can be solved, i want the if statment to be breaked so the program could go to the next question
- 06-01-2010, 10:09 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
From the looks of it you don't need the break... just end the if.
- 06-01-2010, 10:17 PM #5
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
without the break it can move to the next question and it be for the first question.
- 06-01-2010, 10:20 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Can you post the entire code/method? Without knowing exactly what is going on its hard to help.
- 06-01-2010, 10:23 PM #7
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
PHP Code:public class easy extends javax.swing.JFrame { public easy() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); jLabel1 = new javax.swing.JLabel(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jRadioButton3 = new javax.swing.JRadioButton(); jRadioButton4 = new javax.swing.JRadioButton(); jLabel2 = new javax.swing.JLabel(); jButton2 = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); addWindowListener(new java.awt.event.WindowAdapter() { public void windowOpened(java.awt.event.WindowEvent evt) { formWindowOpened(evt); } }); jButton2.setText("Next"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton1.setText("Exit"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(19, 19, 19) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jRadioButton2) .addComponent(jRadioButton1) .addComponent(jRadioButton3) .addComponent(jRadioButton4)) .addContainerGap(364, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addContainerGap(394, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(198, Short.MAX_VALUE) .addComponent(jLabel2) .addGap(206, 206, 206)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 274, Short.MAX_VALUE) .addComponent(jButton1) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(jRadioButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jRadioButton4) .addGap(76, 76, 76) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton2) .addComponent(jButton1)) .addContainerGap()) ); pack(); }// </editor-fold> private void formWindowOpened(java.awt.event.WindowEvent evt) { jLabel1.setText("whats my name?"); jRadioButton1.setText("aziz"); jRadioButton2.setText("fahad"); jRadioButton3.setText("Abdulla"); jRadioButton4.setText("Khaled"); buttonGroup1.add(jRadioButton1); buttonGroup1.add(jRadioButton2); buttonGroup1.add(jRadioButton3); buttonGroup1.add(jRadioButton4); buttonGroup2.add(jRadioButton1); buttonGroup2.add(jRadioButton2); buttonGroup2.add(jRadioButton3); buttonGroup2.add(jRadioButton4); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { if (jRadioButton1.isSelected()) { jLabel2.setText(null); jLabel1.setText("how old am I"); jRadioButton1.setText("15"); jRadioButton2.setText("16"); jRadioButton3.setText("17"); jRadioButton4.setText("18"); buttonGroup1.clearSelection(); } else jLabel2.setText("wrong"); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new easy().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JRadioButton jRadioButton3; private javax.swing.JRadioButton jRadioButton4; // End of variables declaration }
- 06-01-2010, 10:29 PM #8
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
The method ends correctly, why would you need the break?
- 06-01-2010, 10:32 PM #9
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
i want to add another if statment so it can be used to the second question.
- 06-01-2010, 10:34 PM #10
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
You can use a return statement to end the method.
- 06-02-2010, 12:22 AM #11
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
why can't you do smth like
and so forth?Java Code:if (jRadioButton1.isSelected()) { // ... } else if (jRadioButton2.isSelected()) { // ... } else if (jRadioButton3.isSelected()) { // ... }
- 06-02-2010, 12:54 AM #12
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
great idea, but if you notice the code i have an else statement that says
and that means in the second question if the true answer was 3 and the user picked the first the label wont be labeled as wrong because in the first question the true answer was the first answerPHP Code:else jLabel2.setText("wrong");Last edited by AbdulAziz Bader; 06-02-2010 at 12:55 AM. Reason: adding
- 06-02-2010, 01:19 AM #13
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
well, you dont HAVE to have
else jLabel2.setText("wrong");
right?
do this:
Java Code:boolean answeredCorrectly = true; if (jRadioButton1.isSelected()) { // ... set answeredCorrectly ... } else if (jRadioButton2.isSelected()) { // ... set answeredCorrectly ... } else if (jRadioButton3.isSelected()) { // ... set answeredCorrectly ... } if (!answeredCorrectly) { jLabel2.setText("wrong"); }
- 06-02-2010, 02:50 AM #14
Or with a chain of if{} else if{} ending the chain with an else{}
If none of the leading if/else if are true then the last else with be executed.
Sort of like a default in a switch statement. (The chaining of if/else if is often used that way when the test is not for an int == to something)
- 06-02-2010, 06:01 AM #15
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
iluxa, thnx ill try it.
Norm, sorry didn`t get it, could you explain more ?
- 06-02-2010, 07:53 AM #16
Member
- Join Date
- May 2010
- Posts
- 18
- Rep Power
- 0
- 06-02-2010, 03:20 PM #17
I guess I don't understand what the program is supposed to do.
If the user selects the wrong radio button for a question, what is supposed to happen?
How would the program move directly to the 3rd question, skipping the second question?
- 06-02-2010, 03:42 PM #18
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
I think you're logic behind the program is all wrong. How do you determine what question is being answered?
It doesn't look like you are holding a reference to what is being asked anywhere. I recommend using a variable for holding what question number you are on, such as:
You could then use different methods for asking and checking the answer depending on the question Number. Example:Java Code:int questionNumber = 0;
I would recommend making a method which organizes the asking of questions when the next button is clicked, such asJava Code://Examples for asking questions... private void askQuestion1() { jLabel1.setText("whats my name?"); jRadioButton1.setText("aziz"); jRadioButton2.setText("fahad"); jRadioButton3.setText("Abdulla"); jRadioButton4.setText("Khaled"); } private void askQuestion2() { jLabel1.setText("how old am I"); jRadioButton1.setText("15"); jRadioButton2.setText("16"); jRadioButton3.setText("17"); jRadioButton4.setText("18"); } //Examples for checking answers private boolean isQuestion1Correct() { return jRadioButton1.isSelected(); } private boolean isQuestion2Correct() { return jRadioButton2.isSelected(); }
You could do the same thing for checking the answers of the question.Java Code:private void askNextQuestion() { questionNumber++; switch (questionNumber) { case 1: askQuestion1(); break; case 2: askQuestion2(); break; //And so on and so on. default: break; }
Now when the Submit button or Next button is clicked(Wherever you are trying to verify the answer. It could look like this:
Java Code:private void verifyAnswerAndAsk() { if (isQuestionCorrect()) { askNextQuestion(); } else { //Set the jlabel to wrong or take other appropriate action } }
Similar Threads
-
Misunderstanding of Break
By Steve Whalen in forum New To JavaReplies: 6Last Post: 10-22-2008, 06:31 PM -
Break to certain class
By Arez in forum New To JavaReplies: 4Last Post: 10-20-2008, 04:13 AM -
Break Clock
By BruenorBH in forum Advanced JavaReplies: 20Last Post: 09-12-2008, 05:27 AM -
How to use Break with a label
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
How to use Break
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks