This is a class that I made to test my if-else statement for calculating withholding tax for gross pay. The error I get is this:
error: variable tax might not have been initialized
System.out.println("Withholding Tax = " + tax);
I initialized the variable tax and covered all of the possible values for gross in the if-else statements. So what am I doing wrong?
import java.util.Scanner; class apples { public static void main(String args[]) { Scanner input = new Scanner(System.in); double gross = input.nextDouble(); double tax; if (0 <= gross && gross <= 300.00) { tax = 0.10; } else if (300.01 <= gross && gross <= 400.00) { tax = 0.12; } else if (400.01 <= gross && gross <= 500.00) { tax = 0.15; } else if (gross >= 500.01) { tax = 0.20; } else { System.out.println("ERROR! Gross pay is less than zero"); } System.out.println("Withholding Tax = " + tax); } }