Kibbs
Kibbs

Reputation: 33

How do I proceed with else if in java?

I´m fairly new to Java and I need to make a code that evaluates if you are liable for a tax deduction following these 3 conditions

My code compiles but it reads it out wrong like if you live less and 50 from work and is single and the assignment is one year it still reads the conditions for the ones that are not single how do I make it read oh you are single let's skip the conditions for the non single persons? or is that how it works if you have any clue.

My code:

import java.util.Scanner;

public class U72C {

    public static void main (String[]args) {
        Scanner input = new Scanner(System.in);
        System.out.println("What is your distance to work?");
        int distance = input.nextInt();
        System.out.println("Do you live alone = 1 or do you live togheter with someone = 2");
        int living = input.nextInt();
        System.out.println("How long are your work assigment");
        int work = input.nextInt();
        if ( distance > 50 ) {
            System.out.println("You do not get tax dedeuction because of your distance to your work place");
        }
        else if (living == 1 && living == 2) {     
        }
        else if (work > 1) {
            System.out.println("You do not get tax deduction because you live alone and your work assigment exceeds 1 year");
        }
        else if (living == 2 ) {
        }
        else if (work > 3) {
            System.out.println("You do not get tax deduction because your work assigment exceeds 3 years and you don't live alone");
        }
        else {
            System.out.println("You get tax deduction");
        }
    }
}

Upvotes: 2

Views: 123

Answers (3)

Yogi
Yogi

Reputation: 1895

import java.util.Scanner;

public class TaxCalculator {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("What are your living standard please answear the questions");
        System.out.println("What is your distance to work?");
        int distance = input.nextInt();

        System.out.println("Do you live alone = 1 or do you live togheter with someone = 2");
        int living = input.nextInt();

        System.out.println("How long are your work assigment");
        int work = input.nextInt();

        if (living == 1 && work > 1 && distance > 50) {
            System.out.println("You are eligible for TAX");
        } else if (living == 2 && work > 3 && distance > 50) {
            System.out.println("You are eligible for TAX");
        } else {
            System.out.println("Enjoy your life without TAX");
        }
    }

}

You condition for Tax eligibility is multiple condition, You need to use logical condition like &&

Eligibility condition is :

a) User should be alone and should leave more than 50 KM and has worked for atleast 1 years

i.e. living ==1 && work>1 &&distance>50

b) User should not be alone and should leave more than 50 KM and has worked for atleast 3 years

i.e. living ==2 && work>3 &&distance>50

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521168

Contrary to the downvote your received, I think your question is tricky, because it seems that your want fine grain feedback for each potential logical flow leading up to a possible tax deduction. This is very much a real world type question, and companies like Intuit (the maker of TurboTax) actually employ engineers who specialize in this sort of thing.

boolean hasDeduction = false;

if (distance > 50) {
    if (living == 1) {
        if (work > 1) {
            String error = "You do not get tax deduction because you live alone " +
                           "and your work assigment exceeds 1 year.";
            System.out.println(error); 
        }
        else {
            hasDeduction = true;
        }
    }
    else if (living == 2) {
        if (work > 3) {
            String error = "You do not get tax deduction because you live together ";
                   error += "and your work assigment exceeds 3 years.";
            System.out.println(error); 
        }
        else {
            hasDeduction = true;
        }
    }
}
else {
    String error = "You do not get tax deduction because of your distance ";
           error += "to your work place.";
    System.out.println(error);
}

if (hasDeduction) {
    System.out.println("You get tax deduction");
}

Note that ideally we would have this code inside an actual method. The error strings could be read from a resource bundle if we were worried about language compatibility.

Upvotes: 2

user3808887
user3808887

Reputation: 319

else if (living == 1 && living == 2) {  

}

This condition you need to use like this

else if (living == 1 || living == 2) {  

}

&& -> means both needed to be true. || -> only one of them needed to be true

Upvotes: 0

Related Questions