勇者有 50 點生命、2 瓶藥水。drink(h) 喝一瓶:藥水少 1 瓶,生命加 30,但最多 100。沒有藥水時什麼都不做(已經寫好)。main 喝了三次。
HP: 100, potions: 0
#include <stdio.h>
struct Hero {
int hp;
int potions;
};
void drink(struct Hero *h) {
if (h->potions == 0) {
return;
}
// 在這裡寫:藥水減 1,hp 加 30,超過 100 就改成 100
}
int main(void) {
struct Hero hero = {50, 2};
drink(&hero);
drink(&hero);
drink(&hero);
printf("HP: %d, potions: %d\n", hero.hp, hero.potions);
return 0;
}
寫好了先點「執行」試試示例,沒問題再點「提交」。