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.*;
publicclass 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;
publicvoid 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);
}
publicvoid 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;
}
}
publicvoid 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
}
publicvoid guessCheck(int guessNumber)
{
if (guessNumber==compNumber){ // compares the user number and the random number generated by the computer
sumPointsA = count*6;
sumPointsB = count*0;
}
elseif (guessNumber != compNumber){ // compares numbers
sumPointsA = count*0;
sumPointsB = count*2;
}
if (guessNumber == compNumber)
showStatus(guessNumber+" is correct. You have "+ sumPointsA+ "points");
elseif(guessNumber != compNumber)
showStatus(guessNumber+" is not correct, it was "+compNumber+"." +" Computer has "+ sumPointsB);
fin(sumPoints);
}
publicvoid fin(int sumPoints)
{
if (count==4)
if (sumPointsA == sumPointsB)
JOptionPane.showMessageDialog( null, "It is a tie"+ sumPointsA + " : " + sumPointsB +"PLAY AGAIN");
elseif
(sumPointsA > sumPointsB)
JOptionPane.showMessageDialog(null, "You Won" +sumPointsA+ " : " +sumPointsB +"PLAY AGAIN");
elseif
(sumPointsA < sumPointsB)
JOptionPane.showMessageDialog(null, "Computer WON! " +sumPointsA + " : " + sumPointsB+ "PLAY AGAIN");
}
}
> 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.
privateint userWins = 0;
privateint compWins = 0;
then modify your code to increment those variables when a win is recorded.
> 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?