要点:
1.利用一个left变量判断还剩几个字母,用chance判断还能猜几次,原来chance放在开头导致一直WA
#include<stdio.h>
#include<string.h>
#define maxn 100
char s[maxn], s2[maxn];
int left, chance,t;
int win, lose;
void guess(char ch)
{int bad = 1;for (int i = 0; i < strlen(s); i++){if (ch == s[i])//若猜中一个字母在字符串中所有该字母都会显露出来{left--;s[i] = ' ';//未防止已经猜出的字母干扰,将已猜出的赋值为空格bad = 0;}}if (bad) chance--;if (chance == 0) lose = 1;if (left == 0) win = 1;
}int main()
{while (scanf("%d", &t),t != -1){ chance = 7;//值都应该放在循环中scanf("%s%s", s, s2);win = lose = 0;left = strlen(s);printf("Round %d\n", t);for (int i = 0; i < strlen(s2); i++){guess(s2[i]);if (win || lose)break;}if (win) printf("You win.\n");else if (lose) printf("You lose.\n");else printf("You chickened out.\n");}return 0;
}
学习心得:
1.要多次判断比较的值要放在循环中