Results 1 to 8 of 8
  1. #1
    grcarr77 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default clock countdown java help

    I am working on a countdown clock that has to prompt for user to enter number of second, display a message for each second and displays "Stopped" when clock reaches zero. he code i have so far only counts up and then displays Stopped at the number I entered. I need it to count down. Can someone please help!!!!!

    package clockcountdown;

    import javax.swing.JOptionPane;

    public class clockcountdown
    {
    public static void main(String[]args)
    {
    int Secs;
    int i = 0;

    Secs = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of seconds"));

    while(i<Secs)

    {
    JOptionPane.showMessageDialog(null, i + " seconds remaining ");
    i++;
    }
    JOptionPane.showMessageDialog(null, " Stopped ");
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: clock countdown java help

    Simple reverse your logic. You currently have a variable which starts at 0 and increases to some value. Make it start at some value and decreases to 0.

  3. #3
    grcarr77 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: clock countdown java help

    i cant figure out how to do that, I am new to programming and for some reason I dont understand

  4. #4
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: clock countdown java help

    When I learn java it's a lot easier for me to learn from examples. This might help, this is not really reversing the logic, it's just a cheap fix.

    Java Code:
    
    import javax.swing.JOptionPane;
    
    public class Countdown 
    {
    public static void main(String[]args) 
    {
    int secs;
    
    
    secs = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of seconds"));
    
    for(int i=0; i<=(secs); i++){ //Loop through everything in the brackets 'secs' times.
    
    
    {
    	int time = secs - i; //Before the time would display 5 when you would like to display 0, so simply subtract the number of seconds
    JOptionPane.showMessageDialog(null, time + " seconds remaining "); 
    }
    }
    JOptionPane.showMessageDialog(null, " Stopped ");
    }
    }
    PS: you'll have to change the class and package to fit yours

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: clock countdown java help

    Quote Originally Posted by grcarr77 View Post
    i cant figure out how to do that, I am new to programming and for some reason I dont understand
    You can work it out if you try.

    You know how to declare variables.
    You know how to assign values to variables.
    You know how to change the value of variables (namely decrement).
    You know how to use loops.

    That's all you need to do this. Think some more or drop the course as it is going to get way harder than this simple task.

  6. #6
    grcarr77 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: clock countdown java help

    Ok but how can i get the stopped message from appearing after each second message and only to appear in place of the 0 at the end?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: clock countdown java help

    The same way you have it know. The loop iterates over and over until it reaches some termination value and stops. Then you print the message.

  8. #8
    grcarr77 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: clock countdown java help

    Thank you I got it , appreciate the help

Similar Threads

  1. Java Countdown Timer
    By JayB in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 03:17 AM
  2. writing a countdown using java
    By stevie171 in forum New To Java
    Replies: 7
    Last Post: 12-01-2010, 04:52 PM
  3. java clock
    By bugmenot in forum JCreator
    Replies: 0
    Last Post: 04-07-2009, 10:25 PM
  4. Need help with Java clock
    By adz666 in forum New To Java
    Replies: 1
    Last Post: 10-15-2008, 02:18 AM
  5. Please Help - Java Date/Countdown Query
    By desktop_doodle in forum New To Java
    Replies: 2
    Last Post: 01-08-2008, 04:53 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
  •