C 程序 - 否则没有以前的 if 错误

  • 本文关键字:if 错误 程序 c
  • 更新时间 :
  • 英文 :


我看不到我缺少什么,在没有if错误的情况下抛出其他内容。 有人可以指出我正确的方向吗? 我错过了什么,它没有看到它显然在寻找的东西? 我正在努力创建一个猜测数字程序,该程序创建一个随机数,然后要求用户输入程序读取的数字,并让用户知道它是太高还是太低,或者他们猜对了。 一旦猜对了数字,就应该打印出来,他们猜对了,用猜的数。 我目前挂断了丢失的其他内容,没有以前的错误。

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


int main() {
int guess;
time_t t;
int randNumber;
int numberOfGuess = 0;

/*srand is used to seed the random number so that it is different every time.  This is giving the
random number an initial base that the rand() can use to offset with a random number.*/
srand(time(&t));
//This will give a number between 0 and 19, so the +1 makes the number 1 through 20.
randNumber = (rand()% 19) +1;
printf("Let's play a guessing game and see how many times it takes you to guess a preselected ");
printf("random number between 1 - 20.n");
printf("Please guess a number between 1 and 20:t", guess);
scanf("%d", &guess);
//The next statement increases the number of guesses by one
numberOfGuess++;
/*This sets the while loop to validate if the guess is not the randNumber, which enables an escape once
the guess is equal to the randNumber*/
while (guess != randNumber){
//If the guess is the randNumber, then I added a break to allow the program to complete.
if (guess == randNumber) {
break;
}
/*If the guess is lower than or equal to 0 or the guess is greater than 20, I print out the statement
letting the user know the number wasn't between the number and to enter a new guess*/
else {
if (guess <= 0 || guess > 20)
printf("nThe number guessed isn't between 1 and 20, please enter a new guess:t");
scanf("%d", &guess);
}
/*If the guess in higher than the randNumber, I let the user know and ask the user to
enter another guess */
else {
if(guess > randNumber) {
printf("nYour guess of %d is too high, guess again:t", guess);
scanf("%d", &guess);
}
/*If the guess is lower that the randNumber, I let the user know and ask the user to
enter another guess*/
else {
if(guess < randNumber) {
printf("nYour guess of %d is too low, guess again:t", guess);
scanf("%d", &guess);
}
}
}
}
printf("nYou guessed it, the number was %d and it took you %d guesses!", guess, numberOfGuess);
return 0;
}

您的程序包含以下内容:

if (guess == randNumber) {
...
}
...
else {
...
}
...
else {
...
}

所以你有一个if后有两个else子句。

比较您有牙套的位置({ ... }(和您想要牙套的位置。

最新更新