Home > Back-end >  Using if else statements for two separate arguments
Using if else statements for two separate arguments

Time:04-27

To start, I am new to Stack Overflow and also new to coding in general, please be gentle with me.

I'm trying to develop a program using Java that will return a specific statement if certain requirements are met by using and if, else if statement. I will post the code below:

public static void main(String[] args) {
    
    double growthRate = 0; //annual economy growth rate
    double inflationValues = 0; //economy inflation
    try (Scanner gRate = new Scanner (System.in)) { //annual growth rate input
        try (Scanner infVal = new Scanner (System.in)) { //inflation value input
            System.out.println("Please enter the annual growth rate as a decimal: ");
            growthRate = gRate.nextDouble();
            
            System.out.println("Please enter the inflation amount as a decimal: ");
            inflationValues = infVal.nextDouble();
        }
    }
    
    if (growthRate < 0.01 && inflationValues < 0.03)
    {
        System.out.println("Recommended policy: Increase welfare spending, reduce personal taxes, and decrease discount rate.");
    } 
    else if (growthRate > 0.01 && inflationValues > 0.03)
    {
        System.out.println("Recommended policy: Reduce business taxes.");
    }
    else if (growthRate > 0.04 && inflationValues < 0.01)
    {
        System.out.println("Recommended policy: Increase personal and business taxes, and decrease discount rate.");
    }
    else if (growthRate > 0.04 && inflationValues > 0.03)
    {
        System.out.println("Recommended policy: Increase discount rate.");
    }
    else 
    {
    System.out.println("No change in economic policy.");
    }
}

}

Issue 1:

The first 'else if' statement:

else if (growthRate > 0.01 && inflationValues > 0.03)
    {
        System.out.println("Recommended policy: Reduce business taxes.");
    }

interferes with the third 'else if' statement:

else if (growthRate > 0.04 && inflationValues > 0.03)
        {
            System.out.println("Recommended policy: Increase discount rate.");
        }

By interferes I mean that when I try to run the program and input the conditions for the third 'else if' statement, it prints the statement for the first 'else if'.

I'm assuming it's because both statements have the condition of greater than, and I know the way I have the code laid out is incorrect because the second 'else if' statement is supposed to only print if the 'if' statement conditions are not met and then the program would just skip over that if the conditions for the next 'else if' were met.

I also tried having two separate instances of if statements (1 if, else if statement for the a conditions), where the 'else if' was just 'else' and had no conditions:

        if (growthRate < 0.01 && inflationValues < 0.03)
        {
            System.out.println("Recommended policy: Increase welfare spending, reduce personal taxes, and decrease discount rate.");
        } 
        else
        {
            System.out.println("Recommended policy: Reduce business taxes.");
        }

and another for the b conditions, where the first 'else if' was replaced by 'if':

        if (growthRate > 0.04 && inflationValues < 0.01)
        {
            System.out.println("Recommended policy: Increase personal and business taxes, and decrease discount rate.");
        }
        else if (growthRate > 0.04 && inflationValues > 0.03)
        {
            System.out.println("Recommended policy: Increase discount rate.");
        }
        else 
        {
        System.out.println("No change in economic policy.");
        }

but that caused more issues than this current code. There is a huge possibility that I just messed up the code for that though.

I'll post the question information below for more context because this might be confusing and I apologize.

a) If the annual growth rate is less than 1%:

If inflation is less than 3%, recommended economic policy is: Increase welfare spending, reduce personal taxes, and decrease discount rate.

Otherwise, recommended economic policy is: Reduce business taxes.

b) If the annual growth rate is greater than 4%:

If inflation is less than 1%, recommended economic policy is: Increase personal and business taxes, and decrease discount rate. If inflation is greater than 3%, recommended economic policy is: Increase discount rate.

Issue 2:

The final 'else' statement doesn't print:

else 
        {
        System.out.println("No change in economic policy.");
        }

The second 'else if' statement prints instead:

else if (growthRate > 0.01 && inflationValues > 0.03)
        {
            System.out.println("Recommended policy: Reduce business taxes.");
        }

So I'm assuming, just like the first issue, it's due to the conflict in the conditions I have set in the second 'else if', but I'm not really sure how to correct this.

Thank you for taking the time to read this. I look forward to any and all responses.

(PS. if anyone knows how to incorporate numbers as percentages rather than decimals, please let me know)

CodePudding user response:

if (the annual growth rate is less than 1%)
{
  if (inflation is less than 3%)
    Increase welfare spending, reduce personal taxes, and decrease discount rate.
  else
    Reduce business taxes.
}
else if (the annual growth rate is greater than 4%)
{
  if (inflation is less than 1%)
    Increase personal and business taxes, and decrease discount rate.
  else if (inflation is greater than 3%)
    Increase discount rate.
}
else
  No change.

CodePudding user response:

Yeah they have to be nested

if(growthRate < 0.01){
  if(inflationValues < 0.01){
    System.out.println("Recommended policy: Increase welfare spending,reduce personal taxes, and decrease discount rate.");
  }else{
    System.out.println("Recommended policy: Reduce business taxes.");
  }

}else if(growthRate < 0.01){
  ...insert here the text conditions

}else {
   System.out.println("No change in economic policy.");
}

CodePudding user response:

Welcome to StackOverflow, to answer your questions:

  1. You either need to nest the first and third else if (as @Alexander Krum has mentioned) or make the condition more strict, i.e.

    growthRate > 0.01 && growthRate < 0.04 && inflationValues > 0.03

  2. You can't comapre against percentages. Right now your code can work with int values simply by using numbers from 1-100 range. Then it would be something like this:

    growthRate > 1 && growthRate < 4 && inflationValues > 3

If for some reason you want to stick with doubles, try dividing the input by 100.

  • Related