The Artima Developer Community
Sponsored Link

Java Answers Forum
PLease Help with counter

1 reply on 1 page. Most recent reply: Feb 28, 2004 8:08 PM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Nancy

Posts: 11
Nickname: ac121107
Registered: Feb, 2004

PLease Help with counter Posted: Feb 28, 2004 6:28 PM
Reply to this message Reply
Advertisement
This is an applet that generates random numbers and the user try to guess them.
It is suppose to keep track of the numbers of times the user and the computer wins and display it on the status line.I do not know how to make a distintion between user wins and computer wins, so the reult of the winnins is not accurate. Also I do not know where in the program to reset the points (I have tried different ways)so when moving to the second game it keeps the previous game points.
Any suggestion is appreciated.
Thank you
Nancy
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Lab4 extends JApplet implements ActionListener {
	
	JTextField input;                                // number enter by user
	JLabel prompt,prompt1, prompt2, prompt3;         // display message on the screen
	int count, countA, countB,guessNumber, compNumber, sumPointsA, sumPointsB,sumPoints,value;
	
	public void init()
	{
		input = new JTextField (4);                 // input box size  
		input.addActionListener(this);              // program will call action performed when number entered in the input box
		prompt = new JLabel ("Guess a number between 1 and 5");             
		prompt1= new JLabel ("If you guess correctly, you earn 6 points,");
		prompt2= new JLabel ("else computer earn 2 points.");
		prompt3= new JLabel ("One game has 4 guesses");
		Container c = getContentPane ();
		c.setLayout (new FlowLayout() );
		c.add (prompt);
		c.add (prompt1);
		c.add (prompt2);
		c.add (prompt3);
		c.add (input);
		
	}
    public void getNumber()
    {
	    compNumber = 0;
	    for (int i = 1; i <= 1; i++) {                  // loops 1 times 
	    value =1 +(int) (Math.random() *5);             // pick random integer between 1 and 5
	    compNumber = value;  
        }
    }
    public void actionPerformed( ActionEvent e )
    {    
	    showStatus("");                                // clear status line
	    int guessNumber = Integer.parseInt(input.getText() );
	    ++ count;                                      // count number of entries
	    input.setText( "" );                           // clear input box
	    getNumber();
	    guessCheck(guessNumber);                       // calls guessCheck
    }
    public void guessCheck(int guessNumber)
    {
    
        if (guessNumber==compNumber){                  // compares the user number and the random number generated by the computer
	         sumPointsA = count*6;
	         sumPointsB = count*0;
        }
        else if (guessNumber != compNumber){           // compares numbers
	        sumPointsA = count*0;
	        sumPointsB = count*2;
        }
	                       
	    if (guessNumber == compNumber)
	       showStatus(guessNumber+" is correct. You have "+ sumPointsA+ "points");
	    else if(guessNumber != compNumber)
	       showStatus(guessNumber+" is not correct, it was "+compNumber+"." +" Computer has "+ sumPointsB);
	       
	   fin(sumPoints);   
        
	}
	public void fin(int sumPoints)
	{
		if (count==4)
		if (sumPointsA == sumPointsB)
		JOptionPane.showMessageDialog( null, "It is a tie"+ sumPointsA + " : " + sumPointsB +"PLAY AGAIN");
		else if
		(sumPointsA > sumPointsB)
		JOptionPane.showMessageDialog(null, "You Won" +sumPointsA+ " : " +sumPointsB   +"PLAY AGAIN");
		else if
		(sumPointsA < sumPointsB)
		JOptionPane.showMessageDialog(null, "Computer WON! " +sumPointsA + " : " + sumPointsB+ "PLAY AGAIN");
		
   }		
}
 


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: PLease Help with counter Posted: Feb 28, 2004 8:08 PM
Reply to this message Reply
> Nancy wrote:
> It is suppose to keep track of the numbers of times the user
> and the computer wins and display it on the status line.I do
> not know how to make a distintion between user wins and
> computer wins, so the reult of the winnins is not accurate.

Create a variable to keep track of user wins and another one to keep track of computer wins.
private int userWins = 0;
private int compWins = 0;

then modify your code to increment those variables when a win is recorded.
public void fin(int sumPoints)
{
  if (count==4)
    if (sumPointsA == sumPointsB)
      JOptionPane.showMessageDialog( null, "It is a tie"+ sumPointsA + " : " + sumPointsB +"PLAY AGAIN");
    else if(sumPointsA > sumPointsB)
    {
      JOptionPane.showMessageDialog(null, "You Won" +sumPointsA+ " : " +sumPointsB   +"PLAY AGAIN");
      userWins++;
    }
    else if (sumPointsA < sumPointsB)
    {
      JOptionPane.showMessageDialog(null, "Computer WON! " +sumPointsA + " : " + sumPointsB+ "PLAY AGAIN");
      compWins++
    }
  }		


> Also I do not know where in the program to reset the points
> (I have tried different ways)so when moving to the second
> game it keeps the previous game points.

Create a method that resets whatever needs to be reset. After a winner has been declared in the old game, call that method. Since it appears that you declare the winner in the fin() method, you probably would call the reset method at the end of the fin() method.

Here are some other things that you need to look at.
Since this is obviously an assignment, here are some suggestions for things that you should look at once you get the basic program working.
1. If a loop can only loop one time, what is the point of using a loop?

2. You created a variable called sumPoints, but you never use it except as an argument to call the fin() method. Since you never even give it an initial value (a real no-no), do you really need it?

3. You created an parameter called sumPoints in the header of the fin() method, but you never use it in the method. Do you really need it?

NOTE #1: The parameter called sumPoints is NOT the same variable in memory as the variable called sumPoints mentioned in my previous question, even though they have the same name. It is generally a bad practice to give parameters the same name as the variables used as their arguments.

NOTE #2: Taking only one or the other out will cause errors. But, as far as I can see, you aren't using either one!

4. You have declared value as an instance variable, but you only use it in the getNumber() method. Does it need to be an instance variable, or could you get by with it just being a local variable?

Flat View: This topic has 1 reply on 1 page
Topic: Tomcat 4.0.3 and IIS Previous Topic   Next Topic Topic: Java and COM Object

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use