您的位置:首页 > 其它

计算瓶酒的数量

2015-10-29 16:21 344 查看
public class SimpleConstructor{

//啤酒2元一瓶,四个瓶盖可换一瓶啤酒,2个空瓶也可换一瓶啤酒,10元最多可以喝多少瓶啤酒?
static int beerPrice=2;	//啤酒的价格
static int price=10;	//10元
static int count=0;		//总瓶数
final static int EXCHANGE=2;		//空瓶兑换
final static int EXCHANGECOVER=4;	//瓶盖兑换
public static void main(String[] args){
int beerBody=price/beerPrice;	//临时瓶数
count=beerBody;
getCount(beerBody,EXCHANGE);
getCount(beerBody,EXCHANGECOVER);
System.out.print(count);

}

/**
* 计算兑换的瓶数(使用递归算法)
* @param beerBody 瓶数
* @param exchange 兑换类型
* @return
*/
public static int getCount(int beerBody,int exchange)
{
if(beerBody<exchange){
return 1;
}else
{
count++;
beerBody=beerBody-exchange+1;
return getCount(beerBody,exchange);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: