共有500瓶汽水,每喝完1瓶后得到1个空瓶子,每3个空瓶子 又可以换1瓶汽水,喝掉以后又得到一个空瓶子,通过编程
计算出总共能喝多少瓶汽水,最后还剩余多少个空瓶子?
package com.lovo.homework;
import javax.swing.JOptionPane;
public class DrinkWater {
public int full=500;
public int empty=0;
public int empty1=0;
public void cal(){
while(true){
full--;
empty++;
empty1++;
if(empty==3){
empty=0;
full++;
}
if(full==0){
break;
}
}
JOptionPane.showMessageDialog(null, "一共喝了:"+empty1+"\n还剩空瓶个数:"+empty);
}
public static void main(String[] args) {
DrinkWater drinkWater=new DrinkWater();
drinkWater.cal();
}
}