代码打出瓶盖问题
我想问问(十块钱去买啤酒,两块钱一瓶,两个瓶子换一瓶,四个瓶盖换一瓶,一共可以买多少瓶那个问题;用程序怎么打出来
我想问问(十块钱去买啤酒,两块钱一瓶,两个瓶子换一瓶,四个瓶盖换一瓶,一共可以买多少瓶那个问题;用程序怎么打出来
2019-07-09
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; } } }
结果是15瓶酒
举报