I am suppose to write a program that generates a random number, ask the user to guess it, keeps track of # of times play, display the user guess and the random number from the computer in the showstatus, and says who wins. I wrote the program, but I can not get it to display the computer number in status line. It always display 0. Please help Thank you
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, guessNumber, compNumber, sumPointsA, sumPointsB,value,sumPoints;
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()
{
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
guessCheck(guessNumber); // calls guessCheck
}
publicvoid guessCheck(int guessNumber)
{
if (guessNumber==compNumber){
sumPointsA = count*6;
sumPointsB = count*0;
}
elseif (guessNumber != compNumber){
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");
}
}
In your programm, you are not calling the method getNumber() any where in the programm. so it is always zero (0).
make a call to method getNumber () just before method call guessCheck(guessNumber); also ti limit the number with in 0-5 you need to RESET the ComputerNUmber to 0 always. it will work.
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
}