0

I'm a beginner at Java, can I get some advice? I currently have some for-loops in my code to try to find the max and min of an array. However, after the for-loops, I tried to print the variable but it is not showing up as defined.

Part of my current code:

int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;
for(index = 0; index < pressure.length; index++) {
        if(pressure[index]<smallest){
             smallest = pressure[index];
             int minPressure = smallest;
        }   

        else if(pressure[index]>largest){
            largest = pressure[index];
            int maxPressure = largest;
    }
}
 System.out.printf("%s%15d%15d%15d\n" , "Maximum", maxCategory, maxPressure, maxMPH);
  • look up "variable scope" because that is your problem -- your variables are declared in a local scope (within an if or else block) and are invisible outside the block. Declare them outside the block so they are visible both in and out of the blocks. – Hovercraft Full Of Eels Jan 19 '20 at 03:09
  • In the future, you should post your complete error message in your question. You should also search this site on the error for similar questions like the one used to close this question. – Hovercraft Full Of Eels Jan 19 '20 at 03:09
  • Thank you so much! That helped a bunch, and I will do that in the future :) – psychsavvy20 Jan 19 '20 at 04:35

0 Answers0