Kodewick
開始學 →
小挑戰←→0 / 70

喝藥水

#067小挑戰約 6 分鐘

勇者有 50 點生命、2 瓶藥水。drink(h) 喝一瓶:藥水少 1 瓶,生命加 30,但最多 100。沒有藥水時什麼都不做(已經寫好)。main 喝了三次。

應該顯示
HP: 100, potions: 0
main.c
#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;
}
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。