The Artima Developer Community
Sponsored Link

Java Answers Forum
PLease help with random numbers

1 reply on 1 page. Most recent reply: Feb 25, 2004 5:52 AM by Viswanatha Basavalingappa

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 random numbers Posted: Feb 24, 2004 3:49 AM
Reply to this message Reply
Advertisement
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.*;
 
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, guessNumber, compNumber, sumPointsA, sumPointsB,value,sumPoints;
	
	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()
    {
	    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
	   
	    guessCheck(guessNumber);                        // calls guessCheck
    }
    public void guessCheck(int guessNumber)
    {
    
        if (guessNumber==compNumber){
	         sumPointsA = count*6;
	         sumPointsB = count*0;
        }
        else if (guessNumber != compNumber){
	        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");
   }		
}
 	
    
	    
	    
		    
	    
	    
	
		
    
	    
	    
	    
	    
	
 


Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: PLease help with random numbers Posted: Feb 25, 2004 5:52 AM
Reply to this message Reply
Hi Nancy,

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.
    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
    }





Viswa
--------

Flat View: This topic has 1 reply on 1 page
Topic: Halting the current working system from a java program Previous Topic   Next Topic Topic: How To Continue With The Code for Sales Program(Part 2)

Sponsored Links



Google
  Web Artima.com   

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