假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依此类推,请问总共喝了多少瓶饮料     各种报错,直接怒了全部加static也报错求实现
      
       public class Demo2 {
static int k=0;//第二轮开始的瓶数
static int a=50;//一开始共计买了多少瓶水
static int b=0;//b=a/3,第一轮下来兑换了多少瓶
static int c=0;//第一轮下来剩下多少瓶
static int sum=50;
public static void drink(int a)
{
if(a>=0)
{
b=a/3;//50/3=16瓶
c=a-b*3;//c=50-16*3=2瓶
k=b+c;//k=16+2=18瓶
a=k;//a=k=18瓶作为下一次循环的a
drink(a);//递归调用玩不来求指教这地方会报java.lang.StackOverflowError
sum+=b;//把b中除出来的数都累加到sum中
}
}
public static void main(String[] args) {
int a=50;
Demo2 demo2=new Demo2();
demo2.drink(a);
System.out.println("总共喝了"+sum+"瓶");

}}

解决方案 »

  1.   

    你的a>=0肯定是恒成立的,递归当然是不会结束的拉。你把a>=0改成a>0看行不行。
    其实你不用那么麻烦。假设你买了a瓶水,只要从里面拿出2个瓶盖,就可以换的1瓶,因为换得那瓶还是有盖子的
      

  2.   


    我改成这样了,结果是84。不知道为什么a=2了,程序还是继续执行public class Demo2 {
    static int k=0;//第二轮开始的瓶数
    static int a=50;//一开始共计买了多少瓶水
    static int b=0;//b=a/3,第一轮下来兑换了多少瓶
    static int c=0;//第一轮下来剩下多少瓶
    static int sum=50;
    public static void drink(int a)
    {
    while(!(a<=2))
    {
    b=a/3;//50/3=16瓶
    c=a-b*3;//c=50-16*3=2瓶
    k=b+c;//k=16+2=18瓶
    a=k;//a=k=18瓶作为下一次循环的a
    sum+=b;//把b中除出来的数都累加到sum中
    drink(a);//递归调用玩不来
    }
    }
    public static void main(String[] args) {
    int a=50;
    Demo2 demo2=new Demo2();
    demo2.drink(a);
    System.out.println("总共喝了"+sum+"瓶");

    }}
      

  3.   

    你的递归写错了,导致递归无法结束,就挂了。错误的原因,是因为你的终止条件错了,变成无法终止了;给你个简单的参考吧:public class DrunkBottle {    public static void main(String[] args) {
            System.out.println("Begin 50, End with: " + drink(50));
        }    private static int drink(int bottles) {
            if (bottles >= 3) { // 3瓶以下没有奖励,不需要递归计算了,这是重要终止条件
                int rewards = bottles / 3; // 奖励
                int sum = drink(rewards + bottles % 3); // 递归喝:用“剩余+奖励”递归的喝
                return rewards * 3 + sum; // 总计:本次喝 + 递归喝
            } else {
                return bottles; // 最后几瓶而已了
            }
        }
    }
      

  4.   


     public class Demo2 {
        static int k=0;//第二轮开始的瓶数
        static int a=50;//一开始共计买了多少瓶水
        static int c=0;//第一轮下来剩下多少瓶
        static int sum=50;
        public static void drink(int a)
        {
            if(a>=3)
            {
                int  b=a/3;//50/3=16瓶
                c=a-b*3;//c=50-16*3=2瓶
                k=b+c;//k=16+2=18瓶
                a=k;//a=k=18瓶作为下一次循环的a
                drink(a);//递归调用玩不来求指教这地方会报java.lang.StackOverflowError
                sum+=b;//把b中除出来的数都累加到sum中
            }        
        }
        public static void main(String[] args) {
            int a=50;
            Demo2 demo2=new Demo2();
            demo2.drink(a);
            System.out.println("总共喝了"+sum+"瓶");
            
        }}要能够换a的数量必须大于等于3,否则无法兑换。所以递归条件为a>=3;
    b不能作为全局变量,否则sum+=b。加都将会是最后一轮的b。和结果有很大的不同。
    如果要把b作为全局变量,sum+=b,必须放在drink的前面。
    其实你可以使用返回类型的。不需要使用那么多全局变量
      

  5.   

    能弱弱个问个问题么。下面那个a=2的时候,为什么while循环还会执行,我debug研究了下,还是循环了好几次
    while(!(a<=2))
            {
                b=a/3;//50/3=16瓶
                c=a-b*3;//c=50-16*3=2瓶
                k=b+c;//k=16+2=18瓶
                a=k;//a=k=18瓶作为下一次循环的a
                sum+=b;//把b中除出来的数都累加到sum中
                drink(a);//递归调用玩不来
            }        
      

  6.   

    谢谢纠错,我一开始只打算把sum弄成全局变量的然后我一写老是错,就想先实现再说,就全加了static- -
      

  7.   

    while(!(a<=2))既然你用递归了,为啥还会有while?应该是: if (a>2) 吧?
      

  8.   

    我是这样写的,结果是74
    public class TestPing {
    public static void main(String[] args){
    int n = 50;
    int x = 0;
    while(n >= 3){
    n = n-3+1;
    x = x+3;
    }
    x = x+n;
    System.out.println("一共可以喝"+x+"瓶。");
    }
    }
      

  9.   


    public class DrinkTest { /**
     * 遞減法
     * @param botles
     * @return
     */
    private int drinkBotles(int botles){
    // 喝的饮料总数
    int totleDrinked =0;
    // 满瓶子数
    int fullBotles=botles;
    // 還能換瓶子的時候
    while(fullBotles>=3){

    //總瓶子數=fullBotles-3+1
    fullBotles=fullBotles-2;
    //已經喝掉的瓶子數
    totleDrinked+=3;
    }
    //將剩下的飲料喝完
    totleDrinked=totleDrinked+fullBotles;

    return totleDrinked;
    }

    /**
     * 遞歸
     * @param botles
     * @return
     */
    private int drinkBotles2(int botles) { // 喝的饮料总数
    int totleDrinked = botles;
    int fullEmpBotles = botles;
    int a = 0;
    int b = 0; while (fullEmpBotles >= 3) {
    a = fullEmpBotles / 3;
    b = fullEmpBotles % 3; fullEmpBotles=a+b;
    totleDrinked += a;
    }
    return totleDrinked;
    } /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub DrinkTest test = new DrinkTest();
    int drinkedBotles = test.drinkBotles(50);
    int drinkedBotles2 = test.drinkBotles2(50); System.out
    .println("Method1:Totle drinked " + drinkedBotles + " botles");
    System.out.println("Method2:Totle drinked " + drinkedBotles2
    + " botles");
    }}
      

  10.   


    public class Demo2 {
    static int a = 50;// 一开始共计买了多少瓶水
    static int b = 0;// b=a/3,第一轮下来兑换了多少瓶
    static int c = 0;// 第一轮下来剩下多少瓶
    static int sum = 50; public static void drink(int a) {
    if (a / 3 > 1) {
    b = a / 3; //此轮喝的水
    c = a % 3; //此轮不能换水的空瓶
    sum += b; //此轮完一共喝的水
    a = b + c; //此轮喝完水后一共剩的空瓶
    drink(a);
    }
    } public static void main(String[] args) {
    drink(a);
    System.out.println("总共喝了" + sum + "瓶"); }}
    把楼主的稍微修改了下。
      

  11.   


    public class DrinkTest { /**
     * 遞減法
     * @param botles
     * @return
     */
    private int drinkBotles(int botles){
    // 喝的饮料总数
    int totleDrinked =0;
    // 满瓶子数
    int fullBotles=botles;
    // 還能換瓶子的時候
    while(fullBotles>=3){

    //總瓶子數=fullBotles-3+1
    fullBotles=fullBotles-2;
    //已經喝掉的瓶子數
    totleDrinked+=3;
    }
    //將剩下的飲料喝完
    totleDrinked=totleDrinked+fullBotles;

    return totleDrinked;
    }

    /**
     * 
     * @param botles
     * @return
     */
    private int drinkBotles2(int botles) { // 喝的饮料总数
    int totleDrinked = botles;
    int fullEmpBotles = botles;
    int a = 0;
    int b = 0; while (fullEmpBotles >= 3) {
    a = fullEmpBotles / 3;
    b = fullEmpBotles % 3; fullEmpBotles=a+b;
    totleDrinked += a;
    }
    return totleDrinked;
    }


    /**
     * 遞歸
     * @param botles
     * @return
     */
    private int drinkBotles3(int botles) { // 喝的饮料总数
    int totleDrinked = 0;
    int fullEmpBotles = botles;
    int a = 0;
    int b = 0; if (fullEmpBotles >= 3) {
    a = fullEmpBotles / 3;
    b = fullEmpBotles % 3; fullEmpBotles=a+b;
    totleDrinked += a;
    totleDrinked+=drinkBotles3(fullEmpBotles);
    }

    return totleDrinked;
    } /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub int testValue =30;
    DrinkTest test = new DrinkTest();
    int drinkedBotles = test.drinkBotles(testValue);
    int drinkedBotles2 = test.drinkBotles2(testValue);
    int drinkedBotles3 = test.drinkBotles3(testValue)+testValue; System.out.println("Method1:Totle drinked " + drinkedBotles + " botles");
    System.out.println("Method2:Totle drinked " + drinkedBotles2+ " botles");
    System.out.println("Method3:Totle drinked " + drinkedBotles3+ " botles");
    }}
      

  12.   

    public class DemoTest {    public static void main(String[] args) {
            int bonus = 0;
            int sum = 0;
            for (int bottle = 50; bottle > 0; bottle--) {
                bonus++;
                sum++;
                if (bonus == 3) {
                    bottle++;
                    bonus = 0;
                }
            }
            System.out.println("一共喝了" + sum + "瓶");
        }
    }就这么简单, 一共喝了74瓶, 楼主给分吧~