Guest User

Untitled

a guest
Oct 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4.  
  5. /*
  6. * To change this license header, choose License Headers in Project Properties.
  7. * To change this template file, choose Tools | Templates
  8. * and open the template in the editor.
  9. */
  10. /**
  11. *
  12. * @author vdanh
  13. */
  14. public class DemoTimer {
  15.  
  16. static Timer timer;
  17. static int interval;
  18. static boolean canBid;
  19.  
  20. public static void main(String[] args) {
  21. interval = 5; //5 sec
  22. canBid = true;
  23. Scanner s = new Scanner(System.in);
  24.  
  25. while (canBid) {
  26. System.out.println("Enter your bid: ");
  27. String in = s.nextLine();
  28. if (canBid) {
  29. interval = 5; //reset each time user input
  30. if (timer == null) {
  31. timer = new Timer();
  32. timer.scheduleAtFixedRate(new TimerTask() {
  33. @Override
  34. public void run() {
  35. setInterval();
  36. }
  37. }, 1000, 1000);
  38. }
  39. System.out.println("You bid $" + in);
  40. } else {
  41. System.out.println("Out of time");
  42. }
  43. }
  44. }
  45.  
  46. private static void setInterval() {
  47. if (interval == 1) {
  48. timer.cancel();
  49. canBid = false;
  50. }
  51. --interval;
  52. }
  53. }
Add Comment
Please, Sign In to add comment