課程
/后端開發(fā)
/Java
/Java入門第一季(IDEA工具)升級版
我想問問(十塊錢去買啤酒,兩塊錢一瓶,兩個瓶子換一瓶,四個瓶蓋換一瓶,一共可以買多少瓶那個問題;用程序怎么打出來
2019-07-09
源自:Java入門第一季(IDEA工具)升級版 3-2
正在回答
ghhkl k kkk mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
import?java.util.Scanner; public?class?answer?{ ?private?int?bottle; ?private?int?lid; ?private?int?exchangeSum; ?private?int?leftBottles; ?private?int?leftLids; ?public?static?void?main(String[]?args)?{ ??for(;;)?{ ???System.out.println("請輸入你的總金額:"); ???Scanner?sc?=?new?Scanner(System.in); ???double?price?=?sc.nextDouble(); ???int?btm?=?(int)?(price/2); ???int?sum?=?btm?+?new?answer().getAnswser(btm,?btm); ???System.out.println(price+"元共可買到"+sum+"瓶酒"); ??} ?} ?public?int?getAnswser(int?wine,int?cover)?{ ??if(wine>=2||cover>=4)?{ ???bottle?=?wine/2; ???lid=cover/4; ???exchangeSum?=?bottle+lid; ???leftBottles?=?wine%2+exchangeSum; ??????leftLids?=cover%4+exchangeSum; ??????System.out.println("本次使用了"+wine+"個酒瓶和"+cover+"個瓶蓋,獲得" ??????+exchangeSum+"瓶啤酒,"+"剩余"+leftBottles+"個瓶子及"+leftLids+"個瓶蓋"); ??????return?exchangeSum+getAnswser(leftBottles,?leftLids); ??}else?{ ???return?0; ??} ?} }
結(jié)果是15瓶酒
?public static void main(String[] args) {??Scanner scan = new Scanner(System.in);??System.out.println("請輸入金額:");??double money = scan.nextDouble();??int beer = (int) (money/2);??int bottle =beer/2;??int bottlecap = beer/4;??System.out.println("可以買"+(beer+bottle+bottlecap)+"瓶");?}
document.write("不知道");
舉報(bào)
0基礎(chǔ)萌新入門第一課,從Java環(huán)境搭建、工具使用、基礎(chǔ)語法開始
2 回答打代碼的問題
4 回答代碼出現(xiàn)問題
2 回答代碼哪里出問題了??
1 回答打代碼
1 回答代碼哪里出了問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2019-11-04
2019-07-14
結(jié)果是15瓶酒
2019-07-09
?public static void main(String[] args) {
??Scanner scan = new Scanner(System.in);
??System.out.println("請輸入金額:");
??double money = scan.nextDouble();
??int beer = (int) (money/2);
??int bottle =beer/2;
??int bottlecap = beer/4;
??System.out.println("可以買"+(beer+bottle+bottlecap)+"瓶");
?}
2019-07-09
document.write("不知道");