我被賦予了一項家庭作業任務,其任務是創建一個簡單的hang子手遊戲。以下是我的代碼。我目前正在嘗試使用getchar()
來請求用戶輸入一個小寫字母,並查看它是否與他們應該猜測的單詞中的某個字母匹配。從我非常有限的經驗來看,我擁有的代碼應該可以工作,並且當我逐步完成程序時,它看起來應該正常運行,但是當我真正運行該程序時,似乎跳過第二個getchar()
。任何人有任何建議或幫助?C getchar()hang子手遊戲
#include "stdio.h"
#include "math.h"
int main(void) {
int(a);
int(b);
float(x);
float(c);
float(e);
int word[4] = {116, 101, 115, 116};
int guess[4];
c == 0;
a == 0;
b == 0;
printf("Welcome to Hangman\n");
printf("Input a word--one lower case letter at a time\n");
printf("Enter Guess: ");
x = getchar();
if (x > 122) {
printf(" Error, character must be a lowercase letter");
} else
if (x < 97) {
printf(" Error, character must be a lowercase letter");
} else
if (x == 116) {
printf("t is correct\n");
printf("%d", word[0]);
printf(" _");
printf(" _");
printf(" %d ", word[3]);
e = getchar();
if (e == 101) {
printf("e is correct\n");
printf("%d", word[0]);
printf(" %d", word[1]);
printf(" _");
printf(" %d ", word[3]);
} else
if (e == 115) {
printf("s is correct\n");
printf("%d", word[0]);
printf(" _");
printf(" %d", word[2]);
printf(" %d", word[0]);
} else {
printf(" You guessed wrong");
}
} else
if (x == 101) {
printf("e is correct\n");
printf("_");
printf(" %d", word[1]);
printf(" _");
printf(" _ ");
} else
if (x == 115) {
printf("s is correct\n");
printf("_");
printf(" _");
printf(" %d", word[2]);
printf(" _ ");
} else {
printf(" You guessed wrong");
}
}
什麼是int(a);你的聲明對於初學者是不正確的...嘗試'int a;'也是'a == 0;'是'a'與零的比較,**不**爲* * a *賦值零,嘗試'a = 0;' –
這是垃圾代碼。 –
'c == 0;'等人。不要做任何事情。 – Biffen