-1
import java.util.*;
class test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] incomes = new int[N];
        for (int i = 0; i < N; i++) {
            incomes[i] = sc.nextInt();
        }
        System.out.println(Arrays.toString(incomes));
        int[] taxes = new int[N];
        for (int i = 0; i < N; i++) {
            taxes[i] = sc.nextInt();
        }
        System.out.println(Arrays.toString(taxes));
        int[] payed = new int[N];
        for (int i = 0; i < N; i++) {
            payed[i] = incomes[i] * taxes[i];
        }
        System.out.println(Arrays.toString(payed));
        int max = payed[0];
        for (int i = 1; i < N; i++) {
            if (payed[i] > max) {
                max = payed[i];
                int count = i;
            }
        }
        System.out.println("Max is " + count);
    }
}

I can print max fine but I cannot print count. I dont see the difference between max and count and why it will not let me print out count. Any help is appreciated

Community
  • 1
  • 1
Thomasd d
  • 5
  • 3

1 Answers1

1

The count variable is defined inside the loop. You need to define it before the loop so that you have access to it after the loop.

int count = 0;
for (int i = 1; i < N; i++) {
    if (payed[i] > max) {
        max = payed[i];
        count = i;
    }
}
System.out.println("Max is " + count);

moreover you probably need count++ instead of count = i;

saeedkazemi
  • 321
  • 1
  • 5