Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default trouble updating my score

    ok so a few days ago I posted a thread about a simple game that was about 500 lines all in one class and was not functioning properly at all. Since then I've done some reading and have tested some things and have now broken it down into 4 different classes. So far all three questions work as well as gameover. However the blasted score keeps giving me trouble. I have tried using the scoreInc() and scoreDec() methods I wrote in the util file but they functioned just the same as when I put score = score + or - 1; so as of right now I have that in trying to eliminate the possibility of my method being wrong... here are the files and I am off to bed. Also I know I have a few unused variables and Strings and components but they are there for my testing purposes.

    QuizOne
    Java Code:
    package com.blue2525989;
     
    import static com.blue2525989.QuizOneUtil.*;
    import static com.blue2525989.QuizClientOne.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class QuizOne extends JFrame{
            public static int score = 0;
            public static int x = 0;
            
            public static String choice;
            public static String mars = "1. Mars ", earth = "2. Earth ", saturn = "3.Saturn";
            public static String green = "1. Green ", blue = "2. Blue ", red = "3. Red";
            public static String cpp = "1. C++ ", java = "2. Java ", python = "3. Python";
            public static String universe = "1. Universe", galaxy = "2. Galaxy ", nebula = "3. Nebula";
            
           public static JLabel output1 = new JLabel("");
           public static JTextArea output1a = new JTextArea("");
           public static JLabel output2 = new JLabel("");
           public static JTextArea output2a = new JTextArea("");
           public static JLabel scoreKeep = new JLabel("Score : " + score);
           public static JLabel banner = new JLabel("Welcome to my Simple game.");
           public static JLabel choices = new JLabel(""+""+"");
           public static JButton b1 = new JButton("");
           public static JButton b2 = new JButton("");
           public static JButton b3 = new JButton("");
                    
        public QuizOne (){
            super("Simple Game");
             
            FlowLayout flo1 = new FlowLayout(FlowLayout.CENTER);
            GridLayout grd3 = new GridLayout(2,1);
             
            JPanel pane1 = new JPanel();
            pane1.setLayout(grd3);
            pane1.add(banner);
            pane1.add(output2);
             
            JPanel pane1a = new JPanel();
            pane1a.setLayout(grd3);
            pane1a.add(output1);
            pane1a.add(output1a);
            
             
            JPanel pane2 = new JPanel();
            pane2.setLayout(flo1);
            pane2.add(choices);
                     
            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(pane1a);
            mainC.add(pane2);
            mainC.add(pane3);
             
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.setVisible(true);
        }    
         
        public static void main(String[] args) {
            new QuizOne();
            new QuizOneLoader();
        }
    }
    QuizClientOne
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizOneUtil.*;
    import static com.blue2525989.QuizOne.*;
    import java.awt.event.*;
    
    public class QuizClientOne{
        
        public static void question1() {
            prntout2("Start of the game, are you ready?");
            prntout1("Space triva:");
            prntout1a("Which is the fourth planet from the sun?");
            choiceSet(mars,earth,saturn);
            buttonSet(mars,earth,saturn);  
            ActionListener act = (event) -> {
              String command = event.getActionCommand();
               if (command.equals(mars)) {
                score = score + 1;
                question2();
            }
            if (command.equals(earth)) {
                score = score - 1;
                question2();
            }
            if (command.equals(saturn)) {
                score = score - 1;
                question2();
            }  
            };
            b1.addActionListener(act);
            b2.addActionListener(act);
            b3.addActionListener(act);
        }
            
        public static void question2() {
            prntout2("Alright here is another question.");
            prntout1("Computer triva:");
            prntout1a("Which language is this program written in?");
            choiceSet(cpp,java,python);
            buttonSet(cpp,java,python);
            scoreKeep();
            ActionListener act2 = (event2) -> {
                String command2 = event2.getActionCommand();
            if (command2.equals(cpp)) {
                score = score - 1;
                question3();
            }
            if (command2.equals(java)) {
                score = score - 1;
                question3();
            }
            if (command2.equals(python)) {
                score = score + 1;
                question3();
            }            
            };
            b1.addActionListener(act2);
            b2.addActionListener(act2);
            b3.addActionListener(act2);
        }
        
        public static void question3() {
            prntout1("Back to the space questions we go.");
            prntout2("Space triva:");
            prntout1a("We live in the Milky Way ______ .");
            choiceSet(universe,galaxy,nebula);
            buttonSet(universe,galaxy,nebula);
            scoreKeep();
            ActionListener act3 = (event3) -> {
                String command3 = event3.getActionCommand();
                if (command3.equals(universe)){
                    score = score - 1;
                    gameOver();
                }
                if (command3.equals(galaxy)) {
                    score = score + 1;
                    gameOver();
                }
                if (command3.equals(nebula)) {
                    score = score - 1;
                    gameOver();
                }
            };
            b1.addActionListener(act3);
            b2.addActionListener(act3);
            b3.addActionListener(act3);
        }
        
        public static void gameOver() {
            prntout1("Thanks for playing");
            prntout1a("You have finished all the questions\n"+
                    "Your final score was:"+score);
            choiceSet(" "," "," ");
            buttonSet(" "," "," ");
            scoreKeep();
        }
    }
    QuizOneLoader
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizClientOne.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizOneLoader{
        public QuizOneLoader(){
           question1();
            } 
        }
    QuizOneUtil
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizOne.*;
    
    public class QuizOneUtil{
        
        public static void prntout1(String s){
        output1.setText(s);
    }
        public static void prntout1a(String s) {
            output1a.setText(s);
        }    
        
        public static void prntout2(String s) {
            output2.setText(s);
        }
        
        public static void prntout2a(String s) {
            output2a.setText(s);
        }
        
        public static void scoreInc() {
            score++;
        }
        
        public static void scoreDec() {
            score--;
        }
        
        public static void scoreKeep() {
            scoreKeep.setText("Score: " + score);
        }
        
        public static void choiceSet(String s, String s2, String s3) {
            choices.setText(s + " " + s2 + " " + s3);
        }
        
        public static void buttonSet(String s, String s2, String s3) {
            b1.setText(s); b2.setText(s2); b3.setText(s3);
        }
        
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: trouble updating my score

    This is the same comment I made on your calculator question.
    You need to remove all that static, or at least all the ones that aren't constants (which should then be marked as final).

    I think we mentioned last time that you ought to have a Question class that represents a single question, complete with options and the correct answer.
    That would remove a lot of that duplicated code in the client.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: trouble updating my score

    This thread looks like a continuation of this one: http://www.java-forums.org/new-java/...somewhere.html

    It was suggested in that previous thread that there should be a Question class. It would replace the questionN methods. There would be one instance of the Question class for each questionX method. There would be a list of the instances of the Question class and controller methods that would advance through the list of Questions.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: trouble updating my score

    Okay I had made them all static because I don't know another way of being able to access them from other classes without doing it like that(I've only learned how to do that in the past few days.).

    So your suggesting that I make question1.java question2.java etc etc for each question? and then have them return a value to the loader class that then increases a counter to load the next question?

    I tried something similar by making them all public int that no matter which answer you chose it x++; and then returned x to a if statement. it started on the question which was the number of questions I had set up in that particular fashion. I was not able to put the return inside the lambda ActionListener so I think it was just returning x right away without me controlling it. That is why I switched them back to the public static void. it allowed me to use question2(); inside the lambda ActionListener to load the next. I had tried several times either trying to increment x or go off score increment or other variables. This is just how I was able to get it to somewhat work. I am open to suggestions however I don't always know what it is you guys are saying but I try to comprehend and apply it.

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

    Default Re: trouble updating my score

    I just fixed my problem. All I did was on the second question reversed the math. where it is supposed to increase I subtracted 1 and where it is supposed to decrease I added 1. This problem only happens on the second question and then reverts back to normal on the third question. Seems to reverse operations every other one. I think I will write some more questions in the current manner to see if it keeps switching the + and - every other one or not. I also moved all the gui stuff out of QuizOne public QuizOne{} and into its own method of quizgui and changed my main to QuizOne().quizgui(); and it still loads the same. The couple books I've read which is all the knowledge I have on Java usually put that stuff in the constructor. Nothing beats some first hand experience for help.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: trouble updating my score

    We're suggesting making a single Question class.
    That will have the question text (a String) and the multiple choice options (a String[] probably), along with noting which one is correct (the index in the array for the correct answer).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: trouble updating my score

    I think I will write some more questions in the current manner
    I suggest that you stop doing it in the current manner and work on using a Question class.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: trouble updating my score

    I would also remove the input and output from your Question class when you create it.
    The actually I/O should be handled via a QuestionHandler of some sort.

    Also, your answer checking code could be greatly reduced by using if/else. Just
    check if the answer is correct. Otherwise do the stuff for an incorrect answer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: trouble updating my score

    Okay I think I have accomplished what you guys are talking about. I am happy with it because it works exactly how I planned. I created multiple question classes. made the minor changes that were suggested. Everything turned out. I did however go ahead and try writing more like I had already done to test a theory. It still didn't work but it was still a good learning process I think(learn from failure, correct?). Still need to go through and remove a couple things that are not used, but as far as functionality it is working.

    anyways here is all my files all working together. thank you for pointing me in right direction.

    QuizOne
    Java Code:
    package com.blue2525989;
     
    import static com.blue2525989.QuizOneUtil.*;
    import static com.blue2525989.QuizClientOneA.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class QuizOne extends JFrame{
            public static int score = 0;
            
            public static String mars = "1. Mars ", earth = "2. Earth ", saturn = "3.Saturn";
            public static String mars2 = "1a. Mars ", earth2 = "2a. Earth ", saturn2 = "3a.Saturn";
            public static String green = "1. Green ", blue = "2. Blue ", red = "3. Red";
            public static String cpp = "1. C++ ", java = "2. Java ", python = "3. Python";
            public static String universe = "1. Universe", galaxy = "2. Galaxy ", nebula = "3. Nebula";
            public static String universe2 = "1a. Universe", galaxy2 = "2a. Galaxy ", nebula2 = "3a. Nebula";
            public static String newGame = "New Game", exit = "Exit";
            
           public static JLabel output1 = new JLabel("");
           public static JTextArea output1a = new JTextArea("");
           public static JLabel output2 = new JLabel("");
           public static JTextArea output2a = new JTextArea("");
           public static JLabel scoreKeep = new JLabel("Score : " + score);
           public static JLabel banner = new JLabel("Welcome to my Simple game.");
           public static JLabel choices = new JLabel(""+""+"");
           public static JButton b1 = new JButton("");
           public static JButton b2 = new JButton("");
           public static JButton b3 = new JButton("");
          
           public void quizgui() {       
             
            FlowLayout flo1 = new FlowLayout(FlowLayout.CENTER);
            GridLayout grd3 = new GridLayout(2,1);
             
            JPanel pane1 = new JPanel();
            pane1.setLayout(grd3);
            pane1.add(banner);
            pane1.add(output2);
             
            JPanel pane1a = new JPanel();
            pane1a.setLayout(grd3);
            pane1a.add(output1);
            pane1a.add(output1a);        
             
            JPanel pane2 = new JPanel();
            pane2.setLayout(flo1);
            pane2.add(choices);
                     
            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(pane1a);
            mainC.add(pane2);
            mainC.add(pane3);
             
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.setVisible(true);   
           }
           
        public QuizOne (){
            super("Simple Game"); 
        }    
         
        public static void main(String[] args) {
            new QuizOne().quizgui();
            new QuizOneLoader();
        }
    }
    QuizOneLoader
    Java Code:
    package com.blue2525989;
    
    
    import static com.blue2525989.QuizClientOneA.question1;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizOneLoader{
        public QuizOneLoader(){
         
            question1();
        }
        }
    QuizClientOne A-G;
    a
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizClientOneB.*;
    import java.awt.event.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizClientOneA {
        
        public static void question1() {
            score = 0;
            prntout2("Start of the game, are you ready?");
            prntout1("Space triva:");
            prntout1a("Which is the fourth planet from the sun?");
            choiceSet(mars,earth,saturn);
            buttonSet(mars,earth,saturn);
            ActionListener act = (event) -> {
             String command = event.getActionCommand();
               if (command.equals(mars)) {
                score = score + 1;
                question2();
            }
            if (command.equals(earth)) {
                score = score - 1;
                question2();
            }
            if (command.equals(saturn)) {
                score = score - 1;
                question2();
            }   
            };
            b1.addActionListener(act);
            b2.addActionListener(act);
            b3.addActionListener(act);
        }
        
    
        public QuizClientOneA() {
            question1();
            
        }
        
        
    }
    b
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizClientOneC.*;
    import java.awt.event.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizClientOneB {
        
        public static void question2() {
            
            prntout2("Alright here is another question.");
            prntout1("Computer triva:");
            prntout1a("Which language is this program written in?");
            choiceSet(cpp,java,python);
            buttonSet(cpp,java,python);
            scoreKeep();
            ActionListener act2 = (event2) -> {
                String command2 = event2.getActionCommand();
            if (command2.equals(cpp)) {
                score = score - 1;
                question3();
            }
            if (command2.equals(java)) {
                score = score + 1;
                question3();
            }
            if (command2.equals(python)) {
                score = score - 1;
                question3();
            }            
            };
            b1.addActionListener(act2);
            b2.addActionListener(act2);
            b3.addActionListener(act2);
        }
      
        public QuizClientOneB() {
            question2();
            
        }
        
        
    }
    c
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizClientOneD.*;
    import static com.blue2525989.QuizClientOneF.question6;
    import java.awt.event.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizClientOneC {
        
        public static void question3() {
            
            
            prntout2("Back to the space questions we go.");
            prntout1("Space triva:");
            prntout1a("We live in the Milky Way ______ .");
            choiceSet(universe2,galaxy2,nebula2);
            buttonSet(universe2,galaxy2,nebula2);
            scoreKeep();
            ActionListener act3 = (event3) -> {
                String command3 = event3.getActionCommand();
                if (command3.equals(universe2)){
                    score = score - 1;
                    question4();
                }
                if (command3.equals(galaxy2)) {
                    score = score + 1;
                    question4();
                }
                if (command3.equals(nebula2)) {
                    score = score - 1;
                    question4();
                }
            };
            b1.addActionListener(act3);
            b2.addActionListener(act3);
            b3.addActionListener(act3);
        }
      
        public QuizClientOneC() {
            question3();
            
        }
        
        
    }
    d
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizClientOneE.*;
    import java.awt.event.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizClientOneD {
        
        public static void question4() {
            
           
            prntout2("Still in space I see.");
            prntout1("Space triva:");
            prntout1a("The Milky Way Galaxy is part of the?");
            choiceSet(universe,galaxy,nebula);
            buttonSet(universe,galaxy,nebula);
            scoreKeep();
            ActionListener act4 = (event4) -> {
                String command4 = event4.getActionCommand();
                if (command4.equals(universe)) {
                    score = score + 1;
                    question5();
                }
                if (command4.equals(galaxy)) {
                    score = score - 1;
                    question5();
                }
                if (command4.equals(nebula)) {
                    score = score - 1;
                    question5();
                }
            };
            b1.addActionListener(act4);
            b2.addActionListener(act4);
            b3.addActionListener(act4);
        }
      
        public QuizClientOneD() {
            question4();
            
        }
        
        
    }
    e
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizClientOneF.*;
    import java.awt.event.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizClientOneE {
        
        public static void question5() {
           
            prntout2("Second to last space question.");
            prntout1("Space triva:");
            prntout1a("Which planet is host to rings made up of small rock \n"+
                    " rock and dust particles?");
            choiceSet(mars2,earth2,saturn2);
            buttonSet(mars2,earth2,saturn2);
            scoreKeep();
            ActionListener act5 = (event5) -> {
                String command5 = event5.getActionCommand();
                if (command5.equals(mars2)) {
                    score = score - 1;
                    question6();
                }
                if (command5.equals(earth2)) {
                    score = score - 1;
                    question6();
                }
                if (command5.equals(saturn2)) {
                    score = score + 1;
                    question6();
                }
            };
            b1.addActionListener(act5);
            b2.addActionListener(act5);
            b3.addActionListener(act5);
        }
      
        public QuizClientOneE() {
            question5();
            
        }
    }
    f
    Java Code:
    package com.blue2525989;
    
    import static com.blue2525989.QuizClientOneG.*;
    import java.awt.event.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizClientOneF {
        
        public static void question6() {
            prntout2("Last space question, are you ready?");
            prntout1("Space triva:");
            prntout1a("Mars is known as the _____ planet.");
            choiceSet(green,blue,red);
            buttonSet(green,blue,red);
            scoreKeep();
            ActionListener act6 = (event6) -> {
                String command6 = event6.getActionCommand();
                if (command6.equals(green)) {
                    score = score - 1;
                    gameOver();
                }
                if (command6.equals(blue)) {
                    score = score - 1;
                    gameOver();
                }
                if (command6.equals(red)) {
                    score = score + 1;
                    gameOver();
                } 
            };
            b1.addActionListener(act6);
            b2.addActionListener(act6);
            b3.addActionListener(act6);
        }
      
        public QuizClientOneF() {
            question6();
            
        }
    }
    g
    Java Code:
    package com.blue2525989;
    
    import java.awt.event.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
    
    public class QuizClientOneG {
        
        public static void gameOver() {
            
            
            prntout1("Thanks for playing");
            prntout1a("You have finished all the questions\n"+
                    "Your final score was:"+score);
            choiceSet(exit," "," ");
            buttonSet(exit,"","");
            scoreKeep();
            ActionListener act99 = (event99) -> {
                String command99 = event99.getActionCommand();
                if (command99.equals(exit)) {
                    System.exit(0);
                }
            };
            b1.addActionListener(act99);
        }
      
        public QuizClientOneG() {
            gameOver();
            
        }
        
        
    }

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

    Default Re: trouble updating my score

    Actually, I don't see any question classes. And you only need one when you write it.

    Also, you still have code like this.

    Java Code:
    if (correct answer) {
       increment score
       next question
    }
    if (incorrect answer) {
       decrement score
       next question
    }
    if (incorrect answer) {
       decrement score
       next question
    }
    Consider this instead

    Java Code:
    if (correct answer) {
       increase score // for correct answer
       tell them it is correct.
    } else {
       tell them it is incorrect
       show correct answer
    }
    next question // for any answer
    Regards,
    Jim
    Last edited by jim829; 11-04-2016 at 07:47 PM. Reason: improved pseudo code
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: trouble updating my score

    You are going in the wrong direction. There should NOT be multiple classes named: QuizClientOneX
    There should be just one Question class with a constructor that takes three arguments: The question to ask, an array of answers, the index to the correct answer.
    The class will need some methods as come up in the designing of the class.

    It might be useful to have the Question class be an inner class to the main driver class that has the methods and variables the Question class may need access to.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: trouble updating my score

    well crap at least I feel like I am going in the right direction here haha.

    I see what you mean that would clean it up a bit on the buttons.

    I tried writing it all in one but I think that is still beyond my powers. I still am unsure how to differentiate between questions all in one class. I thought I was writing a class with my

    Java Code:
    package com.blue2525989;
     
    import static com.blue2525989.QuizClientOneB.*;
    import java.awt.event.*;
    import static com.blue2525989.QuizOne.*;
    import static com.blue2525989.QuizOneUtil.*;
     
    public class QuizClientOneA {
         
        public static void question1() {
            score = 0;
            prntout2("Start of the game, are you ready?");
            prntout1("Space triva:");
            prntout1a("Which is the fourth planet from the sun?");
            choiceSet(mars,earth,saturn);
            buttonSet(mars,earth,saturn);
            ActionListener act = (event) -> {
             String command = event.getActionCommand();
               if (command.equals(mars)) {
                score = score + 1;
                question2();
            }
            if (command.equals(earth)) {
                score = score - 1;
                question2();
            }
            if (command.equals(saturn)) {
                score = score - 1;
                question2();
            }   
            };
            b1.addActionListener(act);
            b2.addActionListener(act);
            b3.addActionListener(act);
        }
         
     
        public QuizClientOneA() {
            question1();
             
        }
         
         
    }
    and then having each one use the next one.

    Okay I understand about the methods taking the arguments. I still need to brush up on the arrays. That is the worst part about self teaching from books is they rush through topics and a lot of them skip them all together. I read through 1 and half books that were concerned mainly with getting to gui's and skipped the core stuff. I finally have a good one though, which has gotten me this far. I think I am going to do some more reading on arrays and using them before coming back and tackling this next hurdle. Thanks for the help so far.
    Last edited by Blue2525989; 11-04-2016 at 07:08 PM.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: trouble updating my score

    how to differentiate between questions all in one class.
    Something like this:
    Java Code:
      ArrayList<Question> listOfQuestions = new ArrayList<>();
      listOfQuestions.add(new Question("the text of the 1st question here", arrayOfAnswersSForQues1, 2));
      listOfQuestions.add(new Question("the text of the 2nd question here", arrayOfAnswersSForQues2, 1));
      listOfQuestions.add(new Question("the text of the 3rd question here", arrayOfAnswersSForQues3, 2));
      listOfQuestions.add(new Question("the text of the 4th question here", arrayOfAnswersSForQues4, 0));
    The text of all the questions is shown above in the list of new Questions being added to the ArrayList

    Now each Question object has the text of the question it is to ask, an array of answers and the index to the correct answer.
    Last edited by Norm; 11-04-2016 at 07:14 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: trouble updating my score

    BTW, in updating the score, don't do anything for a wrong answer. Only bump the score for a correct answer.
    Otherwise, if the player gets say, 5 wrong and 5 right, their score is 0. I don't think that is what you want.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: trouble updating my score

    okay so I know this isn't exactly what you tried to explain but I think I am getting closer. This works but the score starts adding score + score++, score+score+score++ etc etc. I know the problem is in how I set up my excecution of if statements. I wanted to make the question an int and x++ each time u click a choice and then return x. how ever when I tried I couldn't reach the x++ inside my if (command.equals()) statement. Either I need to move all these if statements somewhere else and re write them or scrape them completely and figure something else out. but when I tried to move them outside question one I couldn't reference them like I wanted. How it is setup now it does what I want minus the score increment.

    Java Code:
    class QuizClient extends QuizOne{
        static String[] question = {"Which is the fourth planet from the sun?", //0
        "Which language is this program written in?", //1
        "We live in the Milky Way ______ .", //2
        "The Milky Way Galaxy is part of the?", //3
        "Which planet is host to rings made up of small rock\n"+
                    "rock and dust particles?", //4
        "Mars is known as the _____ planet.", //5 
        "Thanks for playing my game.", //6 
        "your final score was: " + score}; //7
        static String[] questType = {"Space Trivia: ","Computer Trivia: ","Welcome to My Trivia game."}; //0 & 1 ,2
        static String[] ans = {"1. Mars ","2. Earth ","3. Saturn","1.Mars","2.Earth","3.Saturn","1. Green ","2. Blue ","3. Red",//0 1 2/3 4 5/6 7 8
       "1. C++ ","2. Java ","3. Python","1.Universe-a","2.Galaxy-a","3.Nebula-a",//9 10 11/ 12 13 14
       "1. Universe","2. Galaxy ","3. Nebula","New Game","Exit"};//15 16 17/ 18 19
    
        static void question1(String s1, String s2, String s3, String s4, String s5, String s6) {
            prntout2(s1);
            prntout1(s2);
            prntout1a(s3);
            choiceSet(s4,s5,s6);
            buttonSet(s4,s5,s6);
            scoreKeep();
           ActionListener act = (event) -> {
              String command = event.getActionCommand();
               if (command.equals(s4)) {
            }
            if (command.equals(s5)) {
            }
            if (command.equals(s6)) {
            }
            
            if (command.equals(ans[0])) {
                score++;            
                question1(questType[2],questType[1],question[1],ans[9],ans[10],ans[11]);
            }
            else if (command.equals(ans[1])) {
                question1(questType[2],questType[1],question[1],ans[9],ans[10],ans[11]);
            }
            else if (command.equals(ans[2])){
                question1(questType[2],questType[1],question[1],ans[9],ans[10],ans[11]);
            }
            else if (command.equals(ans[9])) {
                question1(questType[2],questType[0],question[2],ans[12],ans[13],ans[14]);
            }
            else if (command.equals(ans[10])) {
                score++;
                question1(questType[2],questType[0],question[2],ans[12],ans[13],ans[14]);
            }
            else if (command.equals(ans[11])) {
                question1(questType[2],questType[0],question[2],ans[12],ans[13],ans[14]);
            }
            else if (command.equals(ans[12])) {
                question1(questType[2],questType[0],question[3],ans[15],ans[16],ans[17]);
            }
            else if (command.equals(ans[13])) {
                score++;
                question1(questType[2],questType[0],question[3],ans[15],ans[16],ans[17]);            
            }
            else if (command.equals(ans[14])) {
                question1(questType[2],questType[0],question[3],ans[15],ans[16],ans[17]);
            }
            else if (command.equals(ans[15])) {
                score++;
                question1(questType[2],questType[0],question[4],ans[3],ans[4],ans[5]);
            }
            else if (command.equals(ans[16])) {
                question1(questType[2],questType[0],question[4],ans[3],ans[4],ans[5]);
            }
            else if (command.equals(ans[17])) {
                question1(questType[2],questType[0],question[4],ans[3],ans[4],ans[5]);
            }
            else if (command.equals(ans[3])) {
               question1(questType[2],questType[0],question[5],ans[6],ans[7],ans[8]); 
            }
            else if (command.equals(ans[4])) {
               question1(questType[2],questType[0],question[5],ans[6],ans[7],ans[8]); 
            }
            else if (command.equals(ans[5])) {
                score++;
                question1(questType[2],questType[0],question[5],ans[6],ans[7],ans[8]);
            }
            else if (command.equals(ans[6])) {
                question1(questType[2],question[6],"Your final score was: " + score,ans[19],"","");
            }
            else if (command.equals(ans[7])) {
                question1(questType[2],question[6],"Your final score was: " + score,ans[19],"","");
            }
            else if (command.equals(ans[8])) {
                question1(questType[2],question[6],"Your final score was: " + score,ans[19],"","");
            }
            else if (command.equals(ans[19])) {
                System.exit(0);
            }
            };
            b1.addActionListener(act);
            b2.addActionListener(act);
            b3.addActionListener(act);
        } 
    }
    Last edited by Blue2525989; 11-06-2016 at 03:07 PM.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: trouble updating my score

    That's a start. However the text of the questions should NOT be defined in the class. The text of a question should be passed to the class's constructor along with the answers and the correct answer. See post#13 for an example. The data passed to the constructor can be extended to include what ever else you want. Start with the minimum and add in other items as you find a need.

    Also the action listener should not be part of the Question class. The one listener should be in the main/driver class. The Question class may need some access methods like one that returns the answer for the current question.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: trouble updating my score

    okay I think I did something similar to what you just said... I moved my actionlistener to my main. moved my arrays to where I initialize everything else normally. and simplified my question class. It works now. keeps score correctly and moves through questions properly. everything I was shooting for. Thank you for your help.

    Java Code:
    package com.blue2525989;
     
    import static com.blue2525989.QuizClient.*;
    import static com.blue2525989.QuizOneUtil.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class QuizOne extends JFrame{
        public static int score = 0;
           
        static String[] question = {"Which is the fourth planet from the sun?", //0
        "Which language is this program written in?", //1
        "We live in the Milky Way ______ .", //2
        "The Milky Way Galaxy is part of the?", //3
        "Which planet is host to rings made up of small rock\n"+
                    "rock and dust particles?", //4
        "Mars is known as the _____ planet.", //5 
        "Thanks for playing my game.", //6 
        "your final score was: " + score}; //7
        static String[] questType = {"Space Trivia: ","Computer Trivia: ","Welcome to My Trivia game."}; //0 & 1 ,2
        static String[] ans = {"1. Mars ","2. Earth ","3. Saturn","1.Mars","2.Earth","3.Saturn","1. Green ","2. Blue ","3. Red",//0 1 2/3 4 5/6 7 8
       "1. C++ ","2. Java ","3. Python","1.Universe-a","2.Galaxy-a","3.Nebula-a",//9 10 11/ 12 13 14
       "1. Universe","2. Galaxy ","3. Nebula","New Game","Exit"};//15 16 17/ 18 19 
    
            
           public static JLabel output1 = new JLabel("");
           public static JTextArea output1a = new JTextArea("");
           public static JLabel output2 = new JLabel("");
           public static JLabel scoreKeep = new JLabel("Score : " + score);
           public static JLabel banner = new JLabel("Welcome to my Simple game.");
           public static JLabel choices = new JLabel(""+""+"");
           public static JButton b1 = new JButton("");
           public static JButton b2 = new JButton("");
           public static JButton b3 = new JButton("");
          
           public void quizgui() {       
             
            FlowLayout flo1 = new FlowLayout(FlowLayout.CENTER);
            GridLayout grd3 = new GridLayout(2,1);
             
            JPanel pane1 = new JPanel();
            pane1.setLayout(grd3);
            pane1.add(banner);
            pane1.add(output2);
             
            JPanel pane1a = new JPanel();
            pane1a.setLayout(grd3);
            pane1a.add(output1);
            pane1a.add(output1a);        
             
            JPanel pane2 = new JPanel();
            pane2.setLayout(flo1);
            pane2.add(choices);
                     
            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(pane1a);
            mainC.add(pane2);
            mainC.add(pane3);
             
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.setSize(500,500);
            frame.setVisible(true);   
           }
           
        public QuizOne (){
            super("Simple Game"); 
        }    
         
        public static void main(String[] args) {
            new QuizOne().quizgui();
            
           question1(questType[2],questType[0],question[0],ans[0],ans[1],ans[2]);
           
           ActionListener act = (event) -> {
              String command = event.getActionCommand();
            
            if (command.equals(ans[0])) {
                score++;            
                question1(questType[2],questType[1],question[1],ans[9],ans[10],ans[11]);
            }
            else if (command.equals(ans[1])) {
                question1(questType[2],questType[1],question[1],ans[9],ans[10],ans[11]);
            }
            else if (command.equals(ans[2])){
                question1(questType[2],questType[1],question[1],ans[9],ans[10],ans[11]);
            }
            else if (command.equals(ans[9])) {
                question1(questType[2],questType[0],question[2],ans[12],ans[13],ans[14]);
            }
            else if (command.equals(ans[10])) {
                score++;
                question1(questType[2],questType[0],question[2],ans[12],ans[13],ans[14]);
            }
            else if (command.equals(ans[11])) {
                question1(questType[2],questType[0],question[2],ans[12],ans[13],ans[14]);
            }
            else if (command.equals(ans[12])) {
                question1(questType[2],questType[0],question[3],ans[15],ans[16],ans[17]);
            }
            else if (command.equals(ans[13])) {
                score++;
                question1(questType[2],questType[0],question[3],ans[15],ans[16],ans[17]);            
            }
            else if (command.equals(ans[14])) {
                question1(questType[2],questType[0],question[3],ans[15],ans[16],ans[17]);
            }
            else if (command.equals(ans[15])) {
                score++;
                question1(questType[2],questType[0],question[4],ans[3],ans[4],ans[5]);
            }
            else if (command.equals(ans[16])) {
                question1(questType[2],questType[0],question[4],ans[3],ans[4],ans[5]);
            }
            else if (command.equals(ans[17])) {
                question1(questType[2],questType[0],question[4],ans[3],ans[4],ans[5]);
            }
            else if (command.equals(ans[3])) {
               question1(questType[2],questType[0],question[5],ans[6],ans[7],ans[8]); 
            }
            else if (command.equals(ans[4])) {
               question1(questType[2],questType[0],question[5],ans[6],ans[7],ans[8]); 
            }
            else if (command.equals(ans[5])) {
                score++;
                question1(questType[2],questType[0],question[5],ans[6],ans[7],ans[8]);
            }
            else if (command.equals(ans[6])) {
                question1(questType[2],question[6],"Your final score was: " + score,ans[19],"","");
            }
            else if (command.equals(ans[7])) {
                question1(questType[2],question[6],"Your final score was: " + score,ans[19],"","");
            }
            else if (command.equals(ans[8])) {
                score++;
                question1(questType[2],question[6],"Your final score was: " + score,ans[19],"","");
            }
            else if (command.equals(ans[19])) {
                System.exit(0);
            }
            };
            b1.addActionListener(act);
            b2.addActionListener(act);
            b3.addActionListener(act);
            } 
    }
    
    class QuizClient extends QuizOne{
        static void question1(String s1, String s2, String s3, String s4, String s5, String s6) {
            prntout2(s1);
            prntout1(s2);
            prntout1a(s3);
            choiceSet(s4,s5,s6);
            buttonSet(s4,s5,s6);
            scoreKeep();
        } 
    }

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: trouble updating my score

    There does not need to be a long chain of if/else if statements. There only needs to be one if and else inside the action listener:

    if (answer is correct for current question)
    handle correct answer
    else
    handle wrong answer

    Then go to the next question
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: trouble updating my score

    hmm I will try when I get back I am going to run my errands for the day.

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

    Default Re: trouble updating my score

    I am not sure how i would do that since the question array is seperate from the ans array. Would i use if (question[1] && ans[2] == event) { score++; next question } else { next question} i am away from my computer and cant try it but i keep thinking about how i could.

Page 1 of 2 12 LastLast

Similar Threads

  1. Player Score
    By glockner in forum New To Java
    Replies: 0
    Last Post: 02-26-2015, 02:54 AM
  2. Test Score
    By alvina32 in forum New To Java
    Replies: 2
    Last Post: 04-20-2012, 10:28 AM
  3. Having trouble updating JList using JComboBox
    By jchinson in forum AWT / Swing
    Replies: 11
    Last Post: 07-03-2011, 08:32 PM
  4. How to do a score board?
    By vlan in forum Java Applets
    Replies: 11
    Last Post: 06-03-2010, 10:10 AM

Posting Permissions

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