无法使if语句/ while循环生效

时间:2017-03-10 02:57:47

标签: c if-statement while-loop

无法使if语句/ while循环生效。如果我让raceChosen成为一个int,它永远不会让我进入选择另一场比赛。当我使用char时if语句不起作用。我完全不知道该解决这个问题的对象。可以提供任何可能提供的援助。

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <time.h>

struct player {   /*a struct to hold the player information.*/
int type[7];
int name[12];
int Smartness[7];
int Strength[7];
int MagicSkills[7];
int Luck[7];
int Dexterity[7];
int lifePoints[3];
}player;



int  main(void)
{
    struct player* ptr;
    srand(time(NULL));
    int numberPlayers,i,j,Smartness,Luck,Strength,Dexterity;
    int MagicSkills,name,slots,x,Hill,City,LevelGround,m,lifePoints;
    lifePoints = 100;
    char raceChosen[1];


    printf("Please enter the number of players you would like");
    scanf("%d",&numberPlayers);  /*take in the number of players*/

    struct player plyr[numberPlayers];
    i = 0;

    while( i<numberPlayers )
    {

    printf("Please enter the race you would like to play as \n"
    "Press elf for Elf \n"
    "Press 1 for Human \n"        /*taking in the race from the user*/
    "Press 2 for Ogre \n"
    "Press 3 for Wizard \n");
    scanf(" %c",&raceChosen);               


    if(raceChosen == "0"){
        printf("Please enter your name \n:");
        scanf("%d", &ptr->name[i]);
        Smartness = rand()%31 + 70;
        printf("%d\n",Smartness);
        scanf("%d", &ptr->Smartness[i]);
        Luck = rand()%41 + 60;
        printf("%d \n",Luck);
        scanf("%d", &ptr->Luck[i]);
        Strength = rand()%50 + 1;
        printf("%d \n",Strength);          /* generating the character */
        scanf("%d", &ptr->Strength[i]);
        Dexterity = rand()%100 + 1;
        printf("%d\n",Dexterity);
        scanf("%d", &ptr->Dexterity[i]);
        MagicSkills = rand()%31 + 50;
        printf("%d \n",MagicSkills);
        scanf("%d", &ptr->MagicSkills[i]);
        printf("\n********\n");

    }


    if(raceChosen == "1"){
        printf("Please enter your name \n:");
        scanf("%d", &ptr->name[i]);
        Smartness = rand()%31 + 70;
        printf("%d\n",Smartness);
        scanf("%d", &ptr->Smartness[i]);
        Luck = rand()%41 + 60;
        printf("%d \n",Luck);
        scanf("%d", &ptr->Luck[i]);
        Strength = rand()%50 + 1;
        printf("%d \n",Strength);
        scanf("%d", &ptr->Strength[i]);
        Dexterity = rand()%100 + 1;
        printf("%d\n",Dexterity);
        scanf("%d", &ptr->Dexterity[i]);
        MagicSkills = rand()%31 + 50;
        printf("%d \n",MagicSkills);
        scanf("%d", &ptr->MagicSkills[i]);
        printf("\n********\n");



    }

    }
    i++;
    }

    }

3 个答案:

答案 0 :(得分:1)

首先,我建议您在提问之前尝试编译代码。

无论如何,让我们一步一步地完成编译错误:

  1. 第43行:将错误的变量类型传递给scanf

    您只需要标准输入中的一个字符,因此不需要字符串数组(char *),只需使用字符变量(char)

    因此char raceChosen[1]变为char raceChosen

  2. 第46和69行:使用==运算符

    比较两个字符串

    这是开始学习C编程基础知识的一个很好的理由。要比较C中的两个字符串,请使用strcmp(first_string, second_string)

    由于raceChosen不再是字符数组,我们不需要strcmp,我们仍然可以使用==但是0和1左右的引号必须是单引号。

    因此,您的if语句变为if(raceChosen == '0')if(raceChosen == '1')。这是比较两个角色的方式。

  3. 第23,24和32行,许多未使用的变量

    我希望你以后再使用它们,因为这是不好的做法。

  4. 第48行:使用未初始化的变量

    在声明后立即将ptr初始化为NULL(ptr = NULL)。

    只是一个通知,你不需要在这里使用指向struct的指针,除非你被要求。一个简单的结构就可以完成这项工作。

  5. 98行:无关闭括号

    是的,就像它说的那样,关闭括号太多了。

答案 1 :(得分:0)

这样改变,我希望它适合你。

int  main(void)
{
    struct player* ptr;
    srand(time(NULL));
    int i,j,Smartness,Luck,Strength,Dexterity;
    int MagicSkills,name,slots,x,Hill,City,LevelGround,m,lifePoints;
    lifePoints = 100;
    char raceChosen[1];
    static int numberPlayers=0; //Changed

    printf("Please enter the number of players you would like");
    scanf("%d",&numberPlayers);  /*take in the number of players*/

    struct player plyr[numberPlayers];
    i = 0;

    while( i<numberPlayers )
    {

    printf("Please enter the race you would like to play as \n"
    "Press elf for Elf \n"
    "Press 1 for Human \n"        /*taking in the race from the user*/
    "Press 2 for Ogre \n"
    "Press 3 for Wizard \n");
    scanf("%c",&raceChosen);               


    if(*raceChosen == '0'){ //Changed
        printf("Please enter your name \n:");
        scanf("%d", &ptr->name[i]);
        Smartness = rand()%31 + 70;
        printf("%d\n",Smartness);
        scanf("%d", &ptr->Smartness[i]);
        Luck = rand()%41 + 60;
        printf("%d \n",Luck);
        scanf("%d", &ptr->Luck[i]);
        Strength = rand()%50 + 1;
        printf("%d \n",Strength);          /* generating the character */
        scanf("%d", &ptr->Strength[i]);
        Dexterity = rand()%100 + 1;
        printf("%d\n",Dexterity);
        scanf("%d", &ptr->Dexterity[i]);
        MagicSkills = rand()%31 + 50;
        printf("%d \n",MagicSkills);
        scanf("%d", &ptr->MagicSkills[i]);
        printf("\n********\n");

    }


    if(*raceChosen == '1'){ //Changed
        printf("Please enter your name 1\n:");
        scanf("%d", &ptr->name[i]);
        Smartness = rand()%31 + 70;
        printf("%d\n",Smartness);
        scanf("%d", &ptr->Smartness[i]);
        Luck = rand()%41 + 60;
        printf("%d \n",Luck);
        scanf("%d", &ptr->Luck[i]);
        Strength = rand()%50 + 1;
        printf("%d \n",Strength);
        scanf("%d", &ptr->Strength[i]);
        Dexterity = rand()%100 + 1;
        printf("%d\n",Dexterity);
        scanf("%d", &ptr->Dexterity[i]);
        MagicSkills = rand()%31 + 50;
        printf("%d \n",MagicSkills);
        scanf("%d", &ptr->MagicSkills[i]);
        printf("\n********\n");



    }

    }
    i++;
    }

答案 2 :(得分:0)

您的代码中需要考虑两件大事,让我们逐一纠正:

  1. char raceChosen [1]; &LT; =&GT;在这里,您已声明了一个大小为1的字符数组

    请注意:Array是相同类型的数据集合,因此当您想要存储至少2个或更多数据时,您可能已经使用了数组,但是在这里您使用的是单个charecter变量,因此您应该避免使用数组。

    正确方法:char raceChosen;

  2. 其次,

     scanf(" %c",&raceChosen);
     if (raceChosen == "0") 
    
  3. 在这里你输入racechosen作为一个字符:&#34;%c&#34;是用于charecter输入,而comapring你用字符串comapring它:&#34; &#34;双引号是字符串。即;你是comapring

        if (raceChosen == "0")
    
           (char)       (string)
    

    这种情况不会成立,因此你不会输入这些if块。

    正确的方法:

       if (raceChosen == '0')
    
            char         char