亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一個輸入不起作用,而其余的則有效

一個輸入不起作用,而其余的則有效

眼眸繁星 2023-08-16 17:51:29
用戶必須輸入購買總金額和年齡,然后計算最終付款。如果總金額達到或超過 100 美元,可享受總價 20% 的折扣。如果年齡為 65 歲或以上,則總價可享受 10% 的折扣。double discount1 = 0.10;double discount2 = 0.20;double totalPrice = 0.0;double finalPrice = 0.0;System.out.print("Enter total amount: ");double purchase = input.nextDouble();System.out.print("Enter age: ");int age = input.nextInt();if (purchase >= 100) {  totalPrice = purchase * discount2;  finalPrice = purchase - totalPrice;  System.out.print("The final amount is $" + finalPrice);}else if (purchase < 100 && age < 65) {  System.out.println("The final amount is $" + purchase);}else if (age >= 65) {  totalPrice = purchase * discount1;  finalPrice = purchase - totalPrice;  System.out.print("The final amount is $" + finalPrice);}用戶輸入 200 作為總金額,輸入 75 作為年齡。輸出應該是 140.00 美元。但是,我收到的輸出為 160.00 美元。
查看完整描述

3 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

我的方法是將所有折扣加在一起,然后在最后相乘一次。


如果需要的話您可以添加其他折扣


double totalDiscount = 0.0;


if (purchase >= 100) {

  totalDiscount += discount2;

}

if (age >= 65) {

  totalDiscount += discount1;

}


totalPrice = purchase * (1.0 - totalDiscount);

System.out.print("The final amount is $" + totalPrice);


查看完整回答
反對 回復 2023-08-16
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

第一個 if 語句將首先執行。因為價格在100以上。所以其他語句不會被執行。嘗試更改 if 表達式,因為這就是它沒有給出您可能期望的結果的問題



查看完整回答
反對 回復 2023-08-16
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

您需要更改以下代碼,


因為當價格超過 100 時,它將首先運行 if 塊,并且不會進入最后一個塊。


所以按以下方式更改它:-


if (purchase >= 100 && age < 65) {

  totalPrice = purchase * discount2;

  finalPrice = purchase - totalPrice;

  System.out.print("The final amount is $" + finalPrice);

}

else if (purchase < 100 && age < 65) {

  System.out.println("The final amount is $" + purchase);

}

else if (purchase < 100 &&age >= 65) {


  totalPrice = purchase * discount1;

  finalPrice = purchase - totalPrice;

  System.out.print("The final amount is $" + finalPrice);

}

else if (age >= 65) {

  totalPrice1 = purchase * discount2;

  totalPrice = purchase * discount1;

  finalPrice = purchase - totalPrice - totalPrice1 ;

  System.out.print("The final amount is $" + finalPrice);

}


查看完整回答
反對 回復 2023-08-16
  • 3 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號