黑马程序员技术交流社区

标题: 测试题 [打印本页]

作者: cxl1694095035    时间: 2016-5-17 12:17
标题: 测试题
import java.util.Scanner;

/*
         *9、28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?(需写出分析思路)
         *
         */
public class Test9 {

        public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);                 // 创建键盘录入对象

                System.out.println("请输入要买多少瓶");                         // 多买多少个人喝的
                int count = sc.nextInt();                                         // 将买的瓶数存储在count中
                getCount(count);
        }

        public static void getCount(int count) {
                int temp = 0;                                                                 // 瓶盖数
                int buyPing = 0;                                                        // 当前瓶数
                int sendPing = 0;
                while (sendPing + buyPing != count) {
                        buyPing++;                                                                 // 多买一瓶
                        temp++;                                                                 // 瓶盖数+1
                        if (temp / 3 == 1) {
                                sendPing++;                                                         // 赠送的瓶+1
                                temp = temp - 2;                                         // 瓶盖重新计算 换了一瓶又多了一个瓶盖temp-3+1,所以是一2
                                                                                                        // 也可以直接temp=1赋值
                        }
                }
                System.out.println("买了: " + buyPing + " 送了: " + sendPing);
        }
}

作者: haizi004    时间: 2016-5-17 12:47
楼主自己写的吗,我觉得思路是没问题的。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2