Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I have also noticed a problem with your 3rd and 4th if statements. How I found it was inserting various print statements to see which parts of the code got executed. If you do not have a debugger then this is the best way to find problems in your code.

    By the way why not just have one big if/else if statement instead of the 4 separate if statements?

  2. #22
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    I went back to using the Random class. Everytime that the program operates it randomally selects 999 or 1000. Is there a way to change that? Thanks al_Marshy, the setOpaque worked. Thanks everyone else for your help.

  3. #23
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by tnixon22 View Post
    Is there a way to change that?
    Of course there is, you use correct code. Therefore your code must be wrong. I cannot make any other comment since you did not post your new code.

  4. #24
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    Random code is selecting 999 or 1000 everytime.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    public class JFrameGuess extends JFrame implements ActionListener
    {
    	final int LENGTH = 600;
    	final int HEIGHT = 200;
    	final int LIMIT = 1000;
    	private int newAnswer, oldAnswer, comNumber, count = 0, selectedNum;
    	private Container gameBoard = getContentPane();
    	JLabel gameInfo = new JLabel("I am thinking of a number between 1 and 1000." +
    											"  Can you guess my number?");
    	JTextField guess = new JTextField(4);
    	JButton startGame = new JButton("Start Game");
    	JButton exit = new JButton("Exit");
    
    	
    	public JFrameGuess()
    		{
    			super("Program #5 - Number Guessing Game");
    			setSize(LENGTH, HEIGHT);
    			setVisible(true);
    			gameBoard.setLayout(new FlowLayout());
    			gameBoard.setBackground(Color.CYAN);
    			gameBoard.add(gameInfo);
    			gameBoard.add(guess);
    			gameBoard.add(startGame);
    			gameBoard.add(exit);
    			gameInfo.setOpaque(true);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			startGame.addActionListener(this);
    			exit.addActionListener(this);
    			Random ranNum = new Random();
    			selectedNum = (ranNum.nextInt(LIMIT) + 1);
    		}
    	
    	public void actionPerformed(ActionEvent e)
    		{
    			Object source = e.getSource();
    			
    			if(source == startGame)
    				{
    						newAnswer = Integer.parseInt(guess.getText());
    						comNumber = selectedNum;
    						int times = count;
    						compareValues(newAnswer, oldAnswer, comNumber, times);
    
    				}
    				
    			else
    				System.exit(0);
    				
    		}
    	
    	public void compareValues(int newAnswer, int oldAnswer ,int comNumber, int count)
    		{
    		
    				if(newAnswer > oldAnswer && newAnswer < comNumber)
    					displayWarmerLessThan(newAnswer, count);
    				else if(newAnswer < oldAnswer && newAnswer > comNumber)
    					displayWarmerGreaterThan(newAnswer, count);
    				else if(newAnswer < oldAnswer && oldAnswer < comNumber)
    					displayColderLessThan(newAnswer, count);
    				else if(newAnswer > oldAnswer && oldAnswer > comNumber)
    					displayColderGreaterThan(newAnswer, count);
    				else 
    					displayWinner(newAnswer);			
    				
    				count++;
    
    		}
    	
    	public void displayWarmerLessThan(int num, int count)
    		{
    				gameBoard.setVisible(true);
    				gameBoard.setBackground(Color.RED);
    				gameInfo.setText(num + " is TOO LOW!  But you are getting WARMER." +
    													"  Enter guess number " + (count + 1));
    				oldAnswer = Integer.parseInt(guess.getText());
    				
    									
    		}
    	public void displayWarmerGreaterThan(int num, int count)
    		{
    				gameBoard.setVisible(true);
    				gameBoard.setBackground(Color.RED);				
    				gameInfo.setText(num + " is TOO HIGH!  But you are getting WARMER." +
    													"  Enter guess number " + (count + 1));
    				oldAnswer = Integer.parseInt(guess.getText());
    		}
    	
    	public void displayColderLessThan(int num, int count)
    		{
    				gameBoard.setVisible(true);
    				gameBoard.setBackground(Color.BLUE);				
    				gameInfo.setText(num + " is TOO LOW!  But you are getting COLDER." +
    													"  Enter guess number " + (count + 1));
    				
    				oldAnswer = Integer.parseInt(guess.getText());
    			
    			
    		}
    				
    	public void displayColderGreaterThan(int num, int count)
    		{
    				gameBoard.setVisible(true);
    				gameInfo.setText(num + " is TOO HIGH!  Ane you are getting COLDER." + 
    													"  Enter guess number " + (count + 1));
    				oldAnswer = Integer.parseInt(guess.getText());
    		}
    		
    	public void displayWinner(int num)
    		{
    			gameBoard.setVisible(true);
    			gameBoard.setBackground(Color.YELLOW);
    			
    			gameInfo.setText("Congratulations " + num + " is the right number.  " +
    									"It took you " + (count + 1) + " attempts to guess my number.");
    
    			
    		}
    
    	
    	public static void main(String[] args)
    		{
    					
    			JFrameGuess GuessingGame = new JFrameGuess();
    			
    		}
    }

  5. #25
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I just ran your code and got 457, 988 and 318 as the selecetedNum. Your assumption is incorrect.

  6. #26
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    Can you tell me why when I put 999, 1000, and even 2000 (even though 1000) is the limit into the guess that it tells me it is correct?

  7. #27
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    999 and 1000 must be breaking those if statements logic and when they all fail you presume the winning number must have been chosen when that might not be the case, instead of passing newNumber to the winningDisplay method pass the comNumber

  8. #28
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Obviously it fails all the if statements and enters the else which goes to the displayWinner method. As I mentioned in a previous post your 3rd and 4th if statements are wrong. compare them with the 1st and second if statements.

  9. #29
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    I'm sorry Junky I guess I missed your earlier post about the 3rd and 4th if statements. I am looking at them, but am not seeing what you find wrong with them. What is it about them that I am not seeing?

  10. #30
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You are comparing the wrong variables. Also, you increment the count variable which is local to the method and not the same as the count variable which is an instance variable and it stays with a value of 1. Since oldAnswer, newAnswer, selectedNum and count are all instance variables and accessible in all methods then why bother passing them as parameters?

Page 2 of 2 FirstFirst 12

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  3. Help with three errors -.-
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 03-30-2010, 06:52 PM
  4. New to this - having errors with static problems
    By afisher300 in forum New To Java
    Replies: 4
    Last Post: 04-20-2009, 11:42 PM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 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
  •