简体   繁体   English

C做while循环与用户选择

[英]C do while loop with user choice

i am new to programming so the code i use is rather limited to what i've learned. 我是编程新手,所以我使用的代码仅限于我所学。 at any rate im developing a scenario in which a user inputs one of two choices that will trigger different sets of code to execute or at least that is what im trying to do. 无论如何,我都会开发一个场景,在该场景中,用户输入两个选项中的一个将触发要执行的不同代码集,或者至少是我要尝试执行的操作。 however as i walk through my code it is executing it as if there were no if statements everything is running as one block. 但是,当我遍历我的代码时,它正在执行它,就好像没有if语句将所有内容作为一个块运行一样。 im pretty sure my logic is ok and that if anything my syntax is iffy. 我很确定我的逻辑还可以,并且如果我的语法有问题的话。 if there is anything you guys could do to help not just solve my issue if possible i'd like to learn rather than just have things fixed. 如果你们有什么能做的不仅可以解决我的问题,我想学习的不仅仅是解决问题。 thank you for your time. 感谢您的时间。

    int main()
{
    int move = 0, pstrength = 10, pdefense = 20, pintelligence = 40, php = 10, estrength = 30,
        edefense = 40, eintelligence = 25, ehp = 10; 

    printf("Battle Start!\n\nYour HP: %d Enemy HP: %d\n\n1 - Attack\n2 - Magic\n\n", php, ehp);

    float pap = (float)pstrength / (float)edefense * 5;
    float pmp = (float)pintelligence / (float)eintelligence * 5, eap = (float)estrength /
        (float)pdefense * 5;

    do {
        printf("Select your move: "); 

/ player will chose what action to preform if action 1 is selected then player attacks if the enemy does not fall to 0 hp the enemy attacks then the loop resets and prompts for a second round of input / / 如果选择了动作1,则玩家将选择要执行的动作,然后如果敌人的生命值未降至0 hp,则玩家会发起攻击,然后敌人会进行循环重置并提示您进行第二轮输入 /

    scanf("%d", &move);
    if (move = 1); {
    printf("\nYou attacked the enemy!\n");
    ehp = ehp - (float)pap;
    if (ehp > 0){
        printf("The enemy attacked you!\n\n");
        php = php - (float)eap;
        printf("Your HP: %dEnemy HP: %d\n\n", php, ehp);
    }
    }
    if (move = 2); {
        printf("\nYou shocked the enemy!\n");
        ehp = ehp - pmp;
        if (ehp > 0){
            printf("The enemy attacked you!\n");
            php = php - (float)eap;
            printf("Your HP: %dEnemy HP: %d\n\n", php, ehp);
        }}

} while (php >= 0 && ehp >= 0);
if (php <= 0)
    printf("You Died!");
else
    printf("You Won!");
return 0;

} }

i ran a debug and whats happening is regardless of my selection if (move=1) or if (move=2) it runs systemically as in if i input 2 at the start it runs as if i input 1 and so on. 我进行了调试,无论选择(move = 1)还是(move = 2),它都会系统运行,就像我在开始时输入2一样,它的运行就像我输入1一样。

  1. There is a ; 有一个; after each of your if statements. 在每个if语句之后。 Remove them. 删除它们。
  2. Equality checking is done via == not = which is assignment. 通过== not =完成分配的相等性检查。

It will look like this : 它看起来像这样:

if (move == 1) { ... }

You have semicolons after your if statement blocks! 如果if语句阻塞,您将得到分号! the correct syntax for if statements is if(condition) {code} if语句的正确语法是if(condition){code}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM