Правильно ли использовать else if?
Мой код показан ниже:
int main() { int counter; int droll1; int droll2; int droll3; int droll4; int tot1; int tot2; char ans=""; srand(time(NULL)); droll1 = (rand()%6); droll2 = (rand()%6); droll1++; droll2++; tot1 = droll1 + droll2; printf("You rolled %d and %d the total is: ",droll1,droll2); printf(" %d ",tot1); printf("\nGuess if the next dice roll will be higher lower or the same (h/l/s) "); scanf("%c",&ans); droll3 = (rand()%6); droll4 = (rand()%6); droll3++; droll4++; tot2 = droll3 + droll4; if(ans='h'){ if(tot2>tot1){ printf("You guessed right. \n"); }else{printf("wrong \n");} }else if(ans='l'){ if(tot2<tot1){ printf("You guessed right. \n"); }else{printf("wrong \n"); } }else if(ans='s'){ if(tot2==tot1){ printf("You guessed right. \n"); }else{printf("wrong \n"); } } printf("The new total is %d ",tot2); return 0; }
Когда я его запускаю. независимо от того, что я ввожу, он работает так, как если бы вход был "h"
Что я уже пробовал:
Когда я удаляю else и использую три ifs, он все еще выполняет все три условия. ans = 'h','s' или 'l'.