对两个单独的参数使用 if else 语句

Using if else statements for two separate arguments

首先,我是 Stack Overflow 的新手,也是一般编码的新手,请多多关照。

我正在尝试使用 Java 开发一个程序,如果通过使用 if, else if 语句满足某些要求,它将 return 一个特定的语句。我将 post 下面的代码:

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.");
    }
}

}

问题 1:

第一个'else if'语句:

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

干扰第三个 'else if' 语句:

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

干扰是指当我尝试 运行 程序并输入第三个 'else if' 语句的条件时,它会打印第一个 'else if' 的语句。

我假设这是因为两个语句都有大于的条件,而且我知道我的代码布局方式不正确,因为第二个 'else if' 语句应该只在 'if' 语句条件不满足,如果满足下一个 'else if' 的条件,程序将跳过它。

我还尝试了两个单独的 if 语句实例(1 个 if,else if 语句用于 a 条件),其中 'else if' 只是 'else' 并且没有条件:

        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.");
        }

和另一个用于 b 条件,其中第一个 'else if' 被 '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.");
        }

但这导致了比当前代码更多的问题。不过,我很可能只是搞砸了代码。

我将 post 下面的问题信息以获取更多上下文,因为这可能会造成混淆,我深表歉意。

a) 如果年增长率小于1%:

如果inflation小于3%,推荐的经济政策是:增加福利支出,减少个人所得税,降低贴现率。

否则,建议的经济政策是:降低营业税。

b) 如果年增长率大于4%:

如果inflation小于1%,推荐的经济政策是:增加个人和企业税收,降低贴现率。 如果inflation大于3%,推荐的经济政策是:提高贴现率。

问题 2:

最后的 'else' 语句不打印:

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

第二个 'else if' 语句改为打印:

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

所以我假设,就像第一个问题一样,这是由于我在第二个问题中设置的条件发生冲突 'else if',但我不确定如何更正此问题。

感谢您花时间阅读本文。我期待所有的回复。

(PS。如果有人知道如何将数字合并为百分比而不是小数,请告诉我)

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.

是的,它们必须嵌套

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.");
}

欢迎来到 Whosebug,回答您的问题:

  1. 您要么需要嵌套第一个和第三个 else if(正如@Alexander Krum 提到的那样),要么使条件更严格,即

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

  2. 您无法比较百分比。现在,您的代码只需使用 1-100 范围内的数字即可处理 int 值。那么它会是这样的:

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

如果出于某种原因你想坚持使用双打,请尝试将 输入 100.