0

I have this program and I declared a variable inside a for loop. Is this bad conventions? I declared it inside so that each time the variable is representing something else through each iteration, is this good?

import java.util.stream.IntStream;

public class Program {      
    public static boolean changeEnough(int[] change, double amountDue) {
        double balance = 0;
        double[] value = {0.25, 0.1, 0.05, 0.01};
        for (int i=0;i <change.length; i++){
         double temp = change[i] * value[i];
            balance += temp;
        }
        if (balance >= amountDue){
            return true;
        }
        else{
            return false;
        }
    }   
}
Tony
  • 387
  • 1
  • 2
  • 14
  • This has been asked many times before: [site:stackoverflow.com java better to declare variable in loop or outside](https://www.google.com/#q=site:stackoverflow.com+java+better+to+declare+variable+in+loop+or+outside). Please do consider doing a search like this one prior to asking. – Hovercraft Full Of Eels Nov 12 '20 at 01:45
  • If you declare a variable inside a loop, then it won't be visible to anything outside the loop. Please search on "java variable scope". – NomadMaker Nov 12 '20 at 03:47

0 Answers0