Search code examples
javaalgorithmjtextfield

How do I record the highest value in a variable


I have a variable and I need to find the highest value that was there. That value constantly changes. I need to put that highest value in a textfield.

//This is full program. Posting the code won't help.

I will try to explain the program:

It's Heads or Tails. You press two buttons, heads and tails. And there is a combo JTextField, where it writes how many times in a row you get it right. And the highest value that was in Combo JTextField I need to put in the Highscore JTextField.


Solution

  • Just add another variable for the maximum:

    SomeType currentValue, maximumValue;
    
    //Encapsulate currentValue and maximumValue
    void setValue(SomeType value){
        currentValue = value;
    
        //update the maximum
        if(value.compareTo(maximumValue) > 0)
            maximumValue = value;
    }
    
    SomeType getMaximum(){
        return maximumValue;
    }
    
    SomeType getCurrent(){
        return currentValue;
    }