/** * NestedIfStatementEx * * This program demonstrates if/else structures. * * @author Brad Rippe */ import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Container; import javax.swing.UIManager; public class NestedIfStatementEx { public static void main(String[] args) { /** DON'T BE CONCERNED WITH THIS SECTION **/ JFrame frame = new JFrame( "Nested If/else Demo" ); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { } frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); final JTextArea textArea = new JTextArea( 35, 35 ); JScrollPane scrollPane = new JScrollPane( textArea ); scrollPane.setPreferredSize( new Dimension( 600, 300 ) ); frame.getContentPane().add( scrollPane, BorderLayout.CENTER ); JButton codeButton = new JButton( "Show Code" ); codeButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { textArea.setFont( new Font("Courier", Font.BOLD, 16 ) ); textArea.setText( "NESTED IF\\ELSE STATEMENTS\n\n" + "if( intValue <= 50 ) {\n\n" + " if( intValue >= 40 ) {\n\n" + " tA.setText( \"You need to spend more time with reading the book!\" );\n\n" + " } else if( intValue >= 30 ) {\n\n" + " tA.setText( \"You really need to spend more time reading the book!\" );\n\n" + " } else {\n\n" + " tA.setText( \"Have you considered a programming course for review?\" );\n\n" + " }\n\n" + "} else if( intValue <= 70 ) {\n\n" + " if( intValue == 70 ) {\n\n" + " tA.setText( \"Ahhhh! You just made it!\" );\n\n" + " } else {\n\n" + " tA.setText( \"Not quite passing yet!\" );\n\n" + " }\n\n" + "} else {\n\n" + " if( intValue >= 90 ) {\n\n" + " tA.setText( \"Excellent!\" );\n\n" + " } else {\n\n" + " tA.setText( \"Very Well Done!\" );\n\n" + " }\n\n" + "}" ); } }); frame.getContentPane().add( codeButton, BorderLayout.SOUTH ); frame.pack(); frame.setVisible(true); testNestedIfElse( frame, textArea ); } public static void testNestedIfElse(JFrame frame, JTextArea tA) { String input = JOptionPane.showInputDialog( frame, "How many points did you receive on the first assignment?\n" ); int intValue = Integer.parseInt( input ); /********* THIS IS THE SECTION OF INTEREST *********/ if( intValue <= 50 ) { if( intValue >= 40 ) { tA.setText( "You need to spend more time with reading the book!" ); } else if( intValue >= 30 ) { tA.setText( "You really need to spend more time reading the book!" ); } else { tA.setText( "Have you considered a programming course for review?" ); } } else if( intValue <= 70 ) { if( intValue == 70 ) { tA.setText( "Ahhhh! You just made it!" ); } else { tA.setText( "Not quite passing yet!" ); } } else { if( intValue >= 90 ) { tA.setText( "Excellent!" ); } else { tA.setText( "Very Well Done!" ); } } /*********** END SECTION ************/ } }