Results 1 to 2 of 2
  1. #1
    danmc is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Counting with a delayed loop and using button to break

    Hello I started using java a couple of days ago for an intro to programming module at uni. The first assignment is a game that uses a random number.

    I'm trying to use a loop to count up in thousands which displays in a textarea using a delay.

    What I am struggling to find is a way to stop or break the loop at a certain point and then assign that value to a variable. Is there an other way I should be using to display the the "count up"?.

    Thanks in advance i've included what I have so far below minus the if statements to round the random numbers to 1000's

    PHP Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    
    public class counter extends Applet implements ActionListener  {
    	
    	
    	private int machine1Total;
    	private int target;
    	private int currentTotal = 0;
    	private Button start, stop;
    	private int divide;
    	private int i;
    	private TextArea winningsDisplay;
    	private int finalTotal;
    	
    	public void init(){
    		machine1Total = (int) (Math.random()*20000) + 2000;
    		target = (int) (Math.random()*40000) + 40000;
    		divide = machine1Total / 1000;
    		setSize(300, 200);
    start = new Button("Start");
    stop = new Button("stop");
    		add(start);
    		add(stop);
    		
    		start.addActionListener(this);
    		stop.addActionListener(this);
    winningsDisplay = new TextArea("",1,4, TextArea.SCROLLBARS_NONE);
    		winningsDisplay.setEnabled(false);
    		add(winningsDisplay);
    			
    		
    	g.drawString ("Machine 1 Total is  " + machine1Total, 50, 100);
    		g.drawString ("Your Target is " + target, 50, 120);
    		g.drawString ("Current total is " + currentTotal, 50, 140);
    		g.drawString ("divide is " + divide, 50, 160);
    		g.drawString ("final total is  " + finalTotal, 50, 180);
    		}
    
    
    public void actionPerformed(ActionEvent event)
    
    {
    	if (event.getSource() == start)
    	
    		for (int i =divide; i>=1;i--){
    			currentTotal = currentTotal + 1000;
    			winningsDisplay.setText(""+currentTotal);
    			start.addActionListener(this);	
    			delay(1000);
    			
    			
    		}
    		if (event.getSource() == stop)
    			i = 0;
    			repaint();
    }
    	
    private void delay(int millis){
    	try {
    		Thread.sleep(millis);
    	} catch	(InterruptedException e) {
    		System.out.println("Sleep exception");
    	}
    	
    }
    
    }

  2. #2
    danmc is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Misunderstanding of Break
    By Steve Whalen in forum New To Java
    Replies: 6
    Last Post: 10-22-2008, 06:31 PM
  2. Break to certain class
    By Arez in forum New To Java
    Replies: 4
    Last Post: 10-20-2008, 04:13 AM
  3. Break Clock
    By BruenorBH in forum Advanced Java
    Replies: 20
    Last Post: 09-12-2008, 05:27 AM
  4. How to use Break with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  5. How to use Break
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •