How can I stop timer and terminate program when result and userEntry are the same number?

Viewed 47
  1. This is the class to generate a random number

The problem i have is whenever the random number and the user entry are the same I want the timer to stop, display the you won message and terminate the program.

If I close the program when the userEntry and result are the same it works but even if the number appeared before it displays the message you lose

That's why I want the timer to stop when the result is the same as the userEntry


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class Pega4Test {
    public static void main(String[] args){
        TalkingClock lottery= new TalkingClock(1000, true);
        lottery.ask();
        Date now = new Date();
        System.out.println(now);
        lottery.start();

        JOptionPane.showMessageDialog(null, "Quit Inner class program!");
        if(lottery.getUserEntry()==lottery.getResult()){
            lottery.stop();
            System.out.println("You Won!!!!");
            System.exit(0);
        }

        System.out.println("You Lost!");
        System.exit(0);

}

static class TalkingClock {
    private int interval;
    private boolean beep;
    private Timer timer;
    private int userEntry;
    private int counter = 1;
    private int result;

    public TalkingClock(int interval, boolean beep) {
        this.interval = interval;
        this.beep = beep;

    }

    public int getUserEntry() {
        return userEntry;
    }

    public void setUserEntry(int userEntry) {
        this.userEntry = userEntry;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    public void start() {
        ActionListener listener = new TimerPrinter();
        timer = new Timer(interval, listener);
        timer.start();
    }

    public void stop(){
        beep=false;
        timer.stop();
    }


    public void ask() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter desired number: ");
        userEntry = input.nextInt();
    }

    public int  getRandomNumber() {
        Random randomNum = new Random();
        result= randomNum.nextInt(5);
        return result;
    }

    public class TimerPrinter implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println("Print Run #" + counter + ": " + getRandomNumber());
            //System.out.println(getResult()+","+ getUserEntry());
            if (beep) {
                Toolkit.getDefaultToolkit().beep();
                counter++;
                }

            }
        }
    }
}
1 Answers

You're printing You lose and exiting outside the if statement:

if(lottery.getUserEntry()==lottery.getResult()){
         lottery.stop();
         System.out.println("You Won!!!!");
         System.exit(0);
     } 
else {

     System.out.println("You Lost!");
     System.exit(0);
}

you should add an else statement.

Related