28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,够28人喝?假如是50人,又需要买多少瓶可乐?
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入人数 :");
int num = sc.nextInt();
int BuyNumber = 0;
if(num <= 3){
BuyNumber = num;
}
else {
for(int x = 3 ; x <num ; x++){
if(x + x/2 >= num){
if((x%2 == 0)&&(x +x/2 == num)){
BuyNumber = x + 1 ;
break;
}
else {
BuyNumber = x;
}
}
}
}
System.out.println(BuyNumber);
}
} |
|