我嘗試運行此程序並使用枚舉從用戶處獲取一個字符串,並將其作爲int 將數組作爲int我試圖讓從用戶scanf函數的數據,比,而不是由它的阿甘向前c編程嘗試獲取scanf中的枚舉詞,並將其放在int數組中
乳寧的程序是這樣的代碼:
#include <stdio.h>
#include <string.h>
int main()
{
enum color { RED = 1, BLUE, BLACK };
char choice[6];
int colors[21];
int i, counter_red = 0, counter_blue = 0, counter_black = 0, x;
for (i = 0; i < 20; i++)
{
printf("Write your favorite color : RED, BLUE or BLACK\n");
scanf_s("%s", &choice);
if (strcmp(choice, "BLACK") == 0)
{
colors[i] = 3;
}
if (strcmp(choice, "BLUE") == 0)
{
colors[i] = 2;
}
if (strcmp(choice, "RED") == 0)
{
colors[i] = 1;
}
}
for (x = 0; x < 20; x++)
{
if (colors[x] == 1)
{
counter_red++;
}
else if (colors[x] == 2)
{
counter_blue++;
}
else if (colors[x] == 3)
{
counter_black++;
}
}
if (counter_red > counter_blue && counter_red > counter_black)
{
printf("%s", "The most popular color is: RED\n");
}
else if (counter_blue > counter_red && counter_blue > counter_black)
{
printf("%s", "The most popular color is: BLUE\n");
}
else if (counter_black > counter_red && counter_blue < counter_black)
{
printf("%s", "The most popular color is: BLACK\n");
}
printf("RED: %d % \nBLUE: %d %\nBLACK : %d % \n", counter_red * 5, counter_blue * 5, counter_black * 5);
return 0;
}
我推薦先學習C基礎知識,網上有很多C的教程,只是Google。 – theadam 2015-01-21 08:36:58