Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Timer;
- import java.util.TimerTask;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author vdanh
- */
- public class DemoTimer {
- static Timer timer;
- static int interval;
- static boolean canBid;
- public static void main(String[] args) {
- interval = 5; //5 sec
- canBid = true;
- Scanner s = new Scanner(System.in);
- while (canBid) {
- System.out.println("Enter your bid: ");
- String in = s.nextLine();
- if (canBid) {
- interval = 5; //reset each time user input
- if (timer == null) {
- timer = new Timer();
- timer.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- setInterval();
- }
- }, 1000, 1000);
- }
- System.out.println("You bid $" + in);
- } else {
- System.out.println("Out of time");
- }
- }
- }
- private static void setInterval() {
- if (interval == 1) {
- timer.cancel();
- canBid = false;
- }
- --interval;
- }
- }
Add Comment
Please, Sign In to add comment