-1
    private int seconds = 5;
    private SpeedMathGo speedMathGo;
    Timer timeRemaining = new Timer();
    TimerTask task = new TimerTask() {
        
        public void run() {
            
            if (speedMathGo.turn == "X") {
                seconds = 5;
                seconds --;
                
                if (seconds == 0) {
                    speedMathGo.turn = "O";
                }
            }
            
            timerLabel.setText("Time remaining: " + seconds + "s");
            
            if (speedMathGo.turn == "O") {
                seconds = 5;
                seconds --;
                
                if (seconds == 0) {
                    speedMathGo.turn = "X";

                }
                }
            
            timerLabel.setText("Time remaining: " + seconds + "s");
        }
    };
    
    public void time() {
        
        timeRemaining.scheduleAtFixedRate(task, 5000, 1000);
    }

This portion of the code is to act as a timer, giving 5s for X and O to answer the math problem on their respective turn. Why is there no countdown when this is ran? If more parts of the class are required, I will be happy to provide.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • You have asked two questions. Both times you got an answer. Not once have you "accepted" either answer by clicking on the checkmark (beside the answer) to indicate the problem has been solved, nor have you left a message indicating the suggestion did not help. – camickr Nov 02 '20 at 20:41

1 Answers1

1

For Swing you should be using a Swing Timer.

  1. You will need a variable like remainingSeconds.

  2. You will need a Swing Timer that generates an event every second.

  3. When you start the Timer you would set the remainingSeconds to 5 and update your display label

  4. Every time the Timer fires you decrement the remainingSeconds and update your display label.

  5. If the remainingSeconds reaches 0, then you a) stop the Timer and b) invoke your "user failed to answer" logic

  6. However if the user does respond you then a) restart the Timer and b) reset the remainingSeconds to 5 to give the user the full 5 seconds to answer and c) update the display label.

See: https://stackoverflow.com/a/7816604/131872 for a basic usage of the Swing Timer that will stop after 10 seconds.

camickr
  • 321,443
  • 19
  • 166
  • 288