Results 1 to 10 of 10
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default help w loop please

    hi so I am trying to test this simple game out. I cannot seem to get to the third question in my game. Should I go about it in a different approach as far as loops are concerned? any pointers or suggestions would be nice.

    Java Code:
    package com.blue2525989;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class TestFrame2 extends JFrame {
    
        public TestFrame2 (){
            int score = 0;
            boolean answered = false;
            String answer = "";
            JLabel output1 = new JLabel("output1");
            JLabel output2 = new JLabel("output2");
            JLabel scoreKeep = new JLabel("Score : " + score);
            JLabel banner = new JLabel("Welcome to my Simple game.");
            JButton b1 = new JButton("Button 1");
            JButton b2 = new JButton("Button 2");
            JButton b3 = new JButton("Button 3");
            
            FlowLayout flo1 = new FlowLayout(FlowLayout.CENTER);
            
            JPanel pane1 = new JPanel();
            pane1.add(banner);
            
            JPanel pane1and2 = new JPanel();
            pane1and2.add(output1);
            
            JPanel pane2 = new JPanel();
            pane2.setLayout(flo1);
            pane2.add(output2);
                    
            JPanel pane3 = new JPanel();
            GridLayout grd1 = new GridLayout(1,4);
            pane3.setLayout(grd1);
            pane3.add(b1);
            pane3.add(b2);
            pane3.add(b3);
            pane3.add(scoreKeep);
            
            JFrame frame = new JFrame("Test Frame 2");
            BorderLayout brd = new BorderLayout();
            frame.setLayout(brd);
            
            Container mainC = frame.getContentPane();
            GridLayout grd2 = new GridLayout(4,1);
            mainC.setLayout(grd2);
            mainC.add(pane1);
            mainC.add(pane1and2);
            mainC.add(pane2);
            mainC.add(pane3);
            
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.setVisible(true);
            
            //make questions
            output1.setText("What is the color of the ocean?");
            output2.setText("1. Green" + "2. Blue" + "3. Red");
            b1.setText("1. Green"); b2.setText("2. Blue"); b3.setText("3. Red");
            ActionListener act = (event) -> {
                String command = event.getActionCommand();
                if (command.equals("1. Green")) {
                    output2.setText("Wrong answer.");                                                
                }
                if (command.equals("2. Blue")) {
                    output2.setText("Correct answer");                                
                }
                if (command.equals("3. Red")) {
                    output2.setText("Wrong answer");                
                }
                if (command.equals("2. Blue")) {
                    //question 2
                    output1.setText("What planet do you live on?");
                    output2.setText("1. Mars 2. Earth 3. Saturn");
                    b1.setText("1. Mars "); b2.setText("2. Earth "); b3.setText("3. Saturn");
                    if (command.equals("1. Mars")) {
                        output2.setText("Wrong answer");
                    }
                    if (command.equals("2. Earth")) {
                        output2.setText("Correct answer");
                    }
                    if (command.equals("3. Saturn")) {
                        output2.setText("Wrong answer");
                    }
                }
                if (command.equals("2. Earth")) {
                //question 3
                output1.setText("What Language is this program written in?");
                output2.setText("1. C++ 2. Java 3. Python");
                b1.setText("C++"); b2.setText("Java"); b3.setText("Python");
                if (command.equals("C++")) {
                    output2.setText("Wrong answer");
                }
                if (command.equals("Java")) {
                    output2.setText("Correct answer");
                }
                if (command.equals("Pyhon")) {
                    output2.setText("Wrong answer");
                }
            }
                
            }; 
            
            b1.addActionListener(act);
            b2.addActionListener(act);
            b3.addActionListener(act);
        }    
        
        public static void main(String[] args) {
            new TestFrame2();
        }
    }

  2. #2
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help w loop please

    also i cant seem to increment the score int. i have tried score++; in several spots with no avail.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: help w loop please

    When you enter the ActionListener the second time, it can't get to the second question code because it will only run if the
    command is "2. Blue." Same for the third question. I recommend you use different action listeners for each button and
    use variables to hold the command strings. Then reset those strings for each run. Doing it the way you are doing it is
    confusing. Also, when you output "correct answer" you immediately change it so it will never be seen.

    Regards,
    Jim
    Last edited by jim829; 10-20-2016 at 04:49 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: help w loop please

    Actually, if you just have 9 conditionals in your action listener at the same level (not nested) it should work. Only set the buttons
    for the next round in the correct answer for each. Also, on the planets you have a trailing space so it won't compare.

    And finally, I don't see where you changed the text of the label for the score. Also score only has local scope
    so you need to make it an instance field.

    EDIT: and you misspelled Python in one of your strings.

    Regards,
    Jim
    Last edited by jim829; 10-20-2016 at 04:57 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help w loop please

    Yeah i noticed the correct answer never being seen. I was going to leave it until i learned how to add a sleep method so it displays for a few seconds before swotching but havent investigated how to do it yet. I will try the string idea when i get home from work. I didnt think of that but i recently made a text version of the game that used that approach and it worked. Ill try and do that on this one. Thanks for the suggestions jim. I will try them out later.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: help w loop please

    EDIT: and you misspelled Python in one of your strings.
    Typing in the same String in many places allows you to misspell one of the Strings and never know it because the compiler does not compare the contents of Strings to see if you spelled them all correctly.
    I suggest that you define a final String with the value you want and then use that String in all the places where you want that String to appear.
    For example:
    Java Code:
      //  In the variable initialize section
      final String GreenOne = "1. Green";
      final String BlueTwo =  "2, Blue";
      final String RedThree = "3. Red";
    
    
    
          output2.setText(GreenOne + BlueTwo + RedThree);
            b1.setText(GreenOne); b2.setText(BlueTwo); b3.setText(RedThree);
            ActionListener act = (event) -> {
                String command = event.getActionCommand();
                if (command.equals(GreenOne)) {
                    output2.setText("Wrong answer.");                                                
                }
                if (command.equals(BlueTwo)) {
                    output2.setText("Correct answer");                                
                }
                if (command.equals(RedThree)) {
                    output2.setText("Wrong answer");                
                }
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help w loop please

    That worked out nicely thanks for the advice both of you. I can not nest the loops inside each other I learned. And declaring all the strings worked much easier and was easier to type in. However I still cant get it to increment score. I have tried score++; in the loops and it didnt work.
    Java Code:
    package com.blue2525989;
     
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class TestFrame2 extends JFrame {
            int score = 0;
            
            String mars = "1. Mars ", earth = "2. Earth ", saturn = "3.Saturn";
            String green = "1. Green ", blue = "2. Blue ", red = "3. Red";
            String cpp = "1. C++ ", java = "2. Java ", python = "3. Python";
        public TestFrame2 (){
            super("Simple Game");
            JLabel output1 = new JLabel("");
            JLabel output2 = new JLabel("");
            JLabel scoreKeep = new JLabel("Score : " + score);
            JLabel banner = new JLabel("Welcome to my Simple game.");
            JButton b1 = new JButton("");
            JButton b2 = new JButton("");
            JButton b3 = new JButton("");
             
            FlowLayout flo1 = new FlowLayout(FlowLayout.CENTER);
             
            JPanel pane1 = new JPanel();
            pane1.add(banner);
             
            JPanel pane1and2 = new JPanel();
            pane1and2.add(output1);
             
            JPanel pane2 = new JPanel();
            pane2.setLayout(flo1);
            pane2.add(output2);
                     
            JPanel pane3 = new JPanel();
            GridLayout grd1 = new GridLayout(1,4);
            pane3.setLayout(grd1);
            pane3.add(b1);
            pane3.add(b2);
            pane3.add(b3);
            pane3.add(scoreKeep);
             
            JFrame frame = new JFrame("Simple Game");
            BorderLayout brd = new BorderLayout();
            frame.setLayout(brd);
             
            Container mainC = frame.getContentPane();
            GridLayout grd2 = new GridLayout(4,1);
            mainC.setLayout(grd2);
            mainC.add(pane1);
            mainC.add(pane1and2);
            mainC.add(pane2);
            mainC.add(pane3);
             
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.setVisible(true);
             
            //make questions
            output1.setText("What is the color of the ocean?");
            output2.setText(green + blue + red);
            b1.setText(green); b2.setText(blue); b3.setText(red);
            
            ActionListener act = (event) -> {
                String command = event.getActionCommand();
                if (command.equals(green)) {
                    output2.setText("Wrong answer.");                                                
                }
            /*    if (command.equals(blue)) {
                                                    
                }*/
                if (command.equals(red)) {
                    output2.setText("Wrong answer");                
                }
                if (command.equals(blue)) {
                    //question 2
                    score++;
                    output1.setText("What planet do you live on?");
                    output2.setText(mars + earth + saturn);
                    b1.setText(mars); b2.setText(earth); b3.setText(saturn);
                    }
                if (command.equals(mars)) {
                        output2.setText("Wrong answer");
                    }
                if (command.equals(earth)) {
                //question 3
                score++;
                output1.setText("What Language is this program written in?");
                output2.setText(cpp + java + python);
                b1.setText(cpp); b2.setText(java); b3.setText(python);
                    }
                if (command.equals(saturn)) {
                        output2.setText("Wrong answer");
                    }
                if (command.equals(cpp)) {
                    output2.setText("Wrong answer");
                }
                if (command.equals(java)) {
                    score++;
                    output2.setText("Correct answer");
                }
                if (command.equals(python)) {
                    output2.setText("Wrong answer");
                }
            }; 
                    
            b1.addActionListener(act);
            b2.addActionListener(act);
            b3.addActionListener(act);
        }    
         
        public static void main(String[] args) {
            new TestFrame2();
        }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: help w loop please

    I still cant get it to increment score.
    Where are you displaying the new value of score after it is incremented?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help w loop please

    Ah man thanks. Yeah i just added scoreKeep.setText("Score :" + score); works now. Thanks for the help guys.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: help w loop please

    Glad you got it working.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 09:27 AM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •