Results 1 to 4 of 4
  1. #1
    Luftwaffle is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Irritating logical error, switch statement problem

    So I programmed this simple history quiz game, and I'm having some problems, specifically with the mechanism I use to check the answers that the user put in.
    I'm using a textfield and a self-made method to take the asnwers and check them.
    The problem is, it only checks the answer for the first case, or question if you will, so let's say the first question's answer is "True" and the second question's answer is "False", after it has checked the first question and moved on to the second, it behaves as if it is still processing the first, I do not know how to progress.
    Thanks in advance for any help offered!

    Java Code:
    /** 
    * @(#)Game_Test.java 
    * 
    * Game_Test Applet application 
    * 
    * @author 
    * @version 1.00 2012/5/7 
    */ 
    
    import java.awt.*; 
    import java.awt.event.*; 
    import java.util.*; 
    import java.applet.*; 
    import javax.swing.*; 
    
    public class Game_TApp_2 extends JFrame{ 
    
    	private JLabel score_label,Question_label, msglabel,msglabel_2;
    	private int APPLET_WIDTH = 900, APPLET_HEIGHT = 100,round_number,index_X; 
    	private JTextField inputLineTX1;
    	private boolean answerTrue,ansTF;
    	private int score=0,nextTX;
    	private String[] TXQuestions;
    	private JPanel main_panel;
    	private JFrame frame;
    	private JButton doAnother;
    	private String userInput;
    	
    
    
    	public  Game_TApp_2() 
    	{ 
    
    		super("History Adventure");
    		
    		nextTX = -1;
    	
    		
    		Container container = getContentPane();
    
    		 
    		TXQuestions	 = new String[5];
    		
    					
    		Question_label = new JLabel ("1.  Cabeza De Vaca is one of the first Spanish explorers to ever reach or discover Texas in the 16th century.");
    
     
    		msglabel= new JLabel();
    		msglabel_2=new JLabel();
    		score_label=new JLabel("Score " + score );
    
    		doAnother= new JButton("Do another?");
    		doAnother.addActionListener(new DoListener());
    		
    	
    		
    		inputLineTX1 = new JTextField(5); 
    		inputLineTX1.addActionListener(new inputListener1()); 
    		
    		main_panel = new JPanel();
    		 
    		main_panel.setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT));     
    		main_panel.setBackground(Color.cyan); 
    		main_panel.setLayout (new FlowLayout()); 
    		main_panel.add (Question_label);
    		main_panel.add (msglabel);
    		main_panel.add(score_label); 
    		main_panel.add (inputLineTX1);
    		main_panel.add(doAnother); 
    		main_panel.add(msglabel_2);
    		container.add (main_panel, BorderLayout.CENTER);
    		setVisible(true);
    		setSize(425,120);
    		
    		
    	}  // constructor 
    
    	
    	public void /*String*/ setQuestions(/*int index*/)//assigns array questions
    	{
    		
    		TXQuestions[0]="1.  Cabeza De Vaca is one of the first Spanish explorers to ever reach or discover Texas in the 16th century."; 
    		TXQuestions[1]="2. Stephen F Austin is a man who brought cows to Texas"; 
    		TXQuestions[2]="3.	Texas became a state in 1845";
    		TXQuestions[3]="4.	The Jose war occurred during 1846-1848 in Texas ";
    		TXQuestions[4]="5.	Texas gained independence in 1809 ";
    	
    		
    	}
    		
    		
    
    
    
    	
    	public boolean checkAnswer(int index, String userAnswer)
    	{
    		 switch (index) {
                case 0:  if (userAnswer.equalsIgnoreCase("True"))
                	     	answerTrue= true;
                	     else 
                	     	answerTrue= false;
                         break;
                case 1:  if (userAnswer.equalsIgnoreCase("False"))
                	     	answerTrue= true;
                	     else 
                	     	answerTrue= false;
                         break;
                case 2: if (userAnswer.equalsIgnoreCase("True"))
                	     	answerTrue= true;
                	     else 
                	     	answerTrue= false;
                	     break;
               	case 3: if (userAnswer.equalsIgnoreCase("False"))
                	     	answerTrue= true;
                	     else 
                	     	answerTrue= false;
                	     break;
                case 4: if (userAnswer.equalsIgnoreCase("False"))
                	     	answerTrue= true;
                	     else 
                	     	answerTrue= false;
                	     break;
    		   }
                
                return answerTrue;
    	}
    	
    	
    	private class inputListener1 implements ActionListener{
    				
    		
    		public void actionPerformed(ActionEvent event) { 
    			
    			int questionNumber=-1;
    		
    			userInput = inputLineTX1.getText();
    			
    			if (questionNumber<4){    //if statement meant to force the switch statement to check the answers for other cases
    			questionNumber++;
    								 }
    		
    			ansTF =checkAnswer(questionNumber,userInput);
    							
    			 	
    			if(ansTF==true){
    			
    			
    				msglabel.setText("Good Job! Correct");
    				score+=20;
    				score_label.setText("Score " + Integer.toString(score));
    			}
    			else 
    				msglabel.setText("Sorry,that is incorrect."); 
    		
    		
    	
    
    		} // end actionPerformed()
    	
    	} //inputListener1 class
    	
    	
    	//listener for question change
    	private class DoListener implements ActionListener
    	{
    		
    		public void actionPerformed(ActionEvent event)
    		{
    		if(nextTX<4){
    			
    		nextTX++;		
    		Question_label.setText(TXQuestions[nextTX]);
    					}
    		else
    		msglabel_2.setText("This is the last question of this set.");
    			
    		}	
    
    
    	}
    }
    *NOTE driver class is required to run this it is as follows
    Java Code:
    public class Game_TApp_2_Tester{
    	
    	public static void main(String[]args){
    		int a;
    
    		Game_TApp_2 TXHist= new Game_TApp_2();
    		TXHist.setQuestions();
    	
    		
    		
    	}
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Irritating logical error, switch statement problem

    after it has checked the first question and moved on to the second, it behaves as if it is still processing the first
    The actionPerformed() method of inputListener1 will *always* check the question at index zero.

    Java Code:
    public void actionPerformed(ActionEvent event) { 
            // This line sets question number to -1.
            // Like everythig else in the method, it will happen every
            // time the method is called
        int questionNumber=-1;  
             
        userInput = inputLineTX1.getText();
                 
        if (questionNumber<4){
                // This line will always increment the question number which is -1 at
                // this point, making it 0.
            questionNumber++;  
        }
             
        ansTF =checkAnswer(questionNumber,userInput);
        // etc
    It is setting the question number to -1 each time that is causing the behaviour you are seeing. Consider removing that line (and addressing the resulting compiler messages).

    Clearly you need a questionNumber variable somewhere. But what you have to think about is where.

  3. #3
    Luftwaffle is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Irritating logical error, switch statement problem

    Thank you so much! At first it did not work when I simply replaced the position of the variable, but then I used a second variable to act in its place, like the if statement checked the value of the second variable and set it to questionNumber and now it works! Thank you so much!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Irritating logical error, switch statement problem

    You're welcome.

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  2. using logical OR in switch
    By Mapisto in forum New To Java
    Replies: 4
    Last Post: 12-02-2011, 03:53 AM
  3. Error With Portion of Switch Statement
    By Cod in forum New To Java
    Replies: 14
    Last Post: 03-24-2011, 12:25 PM
  4. Replies: 3
    Last Post: 09-26-2010, 07:29 AM
  5. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 05:43 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
  •