vendredi 15 décembre 2017

why my if else dont exit the loop?

the problem starts at if statement ("Did you apply for Housing Allowance allowance for this month? yes/no"); after getting answer from user the app still loop again

why my if else don't exit the loop ? all my logic is okay except if statement font brake at all i have tried to use brake statement to jump the if else and stop the code from infinite run but didn't work also

what am i missing ?

public static void main(String[] args) {

    double basic_salary, tax, KWSP, housing_allowance, holder;
    Scanner scan = new Scanner(System.in);
    String answer = null;

    System.out.println("Enter Basic Salary Of Employee: ");
    basic_salary = scan.nextDouble();

    do {

        if (basic_salary <= 6000 && basic_salary >= 1) {

            tax = 0.10 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.5 * basic_salary;
        } else if (basic_salary >= 6001 && basic_salary <= 10000) {

            tax = 0.20 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.5 * basic_salary;;
        } else if (basic_salary >= 10001 && basic_salary <= 14000) {

            tax = 0.25 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.3 * basic_salary;;
        } else {

            tax = 0.30 * basic_salary;
            System.out.println("The compulsory tax =  " + tax + " $");
            housing_allowance = 0.3 * basic_salary;;
        }


        System.out.println("Type the amount For KWSP contribution 11% or 13%  ONLY");
        KWSP = scan.nextDouble();
        holder = KWSP / 100;
        KWSP = holder * basic_salary;
        System.out.println("KWSP contribution= " + KWSP + " $");



        Scanner input = new Scanner(System.in);
        System.out.println("Did you apply for Housing Allowance allowance for this month? yes/no");
        answer = input.nextLine();

        if (answer.equalsIgnoreCase("yes")) {
            System.out.println("");
            System.out.println("");
            System.out.println("Housing Allowance: " + housing_allowance + " $");
            System.out.println("KWSP:contribution: " + KWSP + " $");
            System.out.println("Tax:contribution : " + housing_allowance + " $");
            System.out.println("Gross Salary Of Employee: " + (KWSP + housing_allowance + tax) + " $");
            System.out.println("");
            System.out.println("");


        } else if (answer.equalsIgnoreCase("no")) {
            System.out.println("");
            System.out.println("");
            System.out.println("KWSP:contribution: " + KWSP + " $");
            System.out.println("Tax:contribution : " + housing_allowance + " $");
            System.out.println("Gross Salary Of Employee: " + (KWSP + tax) + " /n");
            System.out.println("");
            System.out.println("");



        } else {
            break;
        }

    } while (basic_salary > 0); //while loop

    System.out.println("salary cannot be Zero or Negative");
}

}

Aucun commentaire:

Enregistrer un commentaire