首先,我是 Stack Overflow 的新手,也是一般編碼的新手,請對我溫柔一點。
我正在嘗試使用 Java 開發一個程式,如果使用 if 和 else if 陳述句滿足某些要求,它將回傳一個特定的陳述句。我將在下面發布代碼:
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.");
}
}
}
問題一:
第一個“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.");
}
但這導致的問題比當前代碼更多。不過,我很有可能只是弄亂了代碼。
我將在下面發布問題資訊以獲取更多背景關系,因為這可能會造成混淆,我深表歉意。
a) 如果年增長率低于 1%:
如果通貨膨脹率低于 3%,推薦的經濟政策是:增加福利支出,減少個人稅收,降低貼現率。
否則,推薦的經濟政策是:降低營業稅。
b) 如果年增長率大于 4%:
如果通貨膨脹率低于 1%,建議的經濟政策是:增加個人和企業稅,并降低貼現率。如果通貨膨脹率大于 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。如果有人知道如何將數字合并為百分比而不是小數,請告訴我)
uj5u.com熱心網友回復:
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.
uj5u.com熱心網友回復:
是的,它們必須嵌套
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.");
}
uj5u.com熱心網友回復:
歡迎來到 StackOverflow,回答您的問題:
您要么需要嵌套第一個和第三個
else if(正如@Alexander Krum 所提到的),要么使條件更嚴格,即growthRate > 0.01 && growthRate < 0.04 && inflationValues > 0.03你不能比較百分比。現在,您的代碼
int只需使用 1-100 范圍內的數字即可處理值。然后它會是這樣的:growthRate > 1 && growthRate < 4 && inflationValues > 3
如果出于某種原因您想堅持使用雙精度數,請嘗試將輸入除以 100。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/466908.html
下一篇:C 非整數表現得像0
