无法在c程序中退出while循环

时间:2017-01-29 02:05:22

标签: c while-loop infinite-loop

我的c程序中有一个while循环的问题,用于c编程的入门课程。我需要告诉计算机它的数字猜测是大还是小,然后当它正确时它应该打印猜测的数量和数量。程序进入while循环后,它将不会退出,我不确定如何解决它。

程序需要做的样本

Enter n: 50
Is your number 25? l
Is your number 38? l
Is your number 44? s
Is your number 41? e
Your number must be 41. I used 4 guesses.

这是我目前的代码:

#include <stdio.h>

int Guess(int upperBound, int lowerBound){
int Guess = 0;
Guess = (lowerBound+upperBound) / 2; // computer's guess is the midpoint 
return Guess;
}

int main(void){

//variables
int Integer = 0;
int lowerBound = 1;
int upperBound = 0;
int Guess = 0;
char choice;
int guessCount = 0;


//enter an integer n
printf("Enter n: ");
scanf("%d" , &Integer);

getchar();

upperBound = Integer;
Guess = Guess(upperBound , lowerBound); //send to function to get computer's guess

// computer prints its choice 'x' and asks if it is larger or smaller than n.
printf("Is your number %d? " , Guess );
scanf("%c" , &choice);

while (choice == 'l' || choice == 's'){
switch (choice){
case 'l':
    lowerBound = Guess + 1;
    ++guessCount;
    Guess = numGuess(upperBound , Guess);
    getchar();
    printf("Is this your number %d? " , Guess);
    getchar();
    break;
case 's':
    upperBound = Guess - 1;
    ++guessCount;
    Guess = numGuess(upperBound , lowerBound);
    getchar();
    printf("Is this your number %d? " , Guess);
    getchar();
    break;
default:
    break;
}
}
//if user enters 'e' then the computer guess right, prints the x value, number of guesses and program ends
if (choice == 'e'){
printf("Your number must be %d. I used %d guesses." , Guess , guessCount);
}
return 0;
}

2 个答案:

答案 0 :(得分:0)

好吧,我修改了一些代码,以便它能够满足您的需求。我建议你停止使用变量名,比如Integer。此外,您应该将每个变量命名为小写,以便维护结构。

希望此代码可以帮助您解决一些问题并开始使用C编码!

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

int Guess(int upperBound, int lowerBound){
    return (lowerBound+upperBound) / 2; // computer's guess is the midpoint 
}

int main(void){

//variables
int number = 0;
int lowerBound = 1;
int upperBound = 0;
int guess = 0;
char choice;
int guessCount = 0;


//enter a number n
printf("Enter n: ");
scanf("%d" , &number);

getchar();

upperBound = rand()%(number*10);
guess = Guess(upperBound , lowerBound); //send to function to get computer's guess

// computer prints its choice 'x' and asks if it is larger or smaller than n.
printf("Is your number %d? " , guess );
scanf("%c" , &choice);
getchar();

while (choice != 'e'){
    if(choice == 'l') {
        lowerBound = guess + 1;         
    } else if (choice == 's') {
        upperBound = guess - 1;            
    }
    guess = Guess(upperBound , lowerBound);
    guessCount++;
    printf("Is this your number %d? " , guess);
    scanf("%c" , &choice);
    getchar();
}
//if user enters 'e' then the computer guess right, prints the x value, number of guesses and program ends
printf("Your number must be %d. I used %d guesses." , guess , guessCount);

return 0;
}

我还鼓励您找到更好,更准确的替代方案来解决同样的问题!祝你好运。

答案 1 :(得分:0)

您似乎是c编程的初学者。扫描后无需使用getchar。

对于你的问题,你只需要输入一次用户输入,注意:它在while循环之外。因此,你无法在内部进行修改,并进入无限循环。另外,你检查选择的最后一部分= e也是在while循环范围之外。

对于这个问题,你可以使用do while循环,如下所示:

 do {
        printf("Is your number %d? " , Guess );
        scanf("%c" , &choice);
        switch (choice){
        case 'l':
        lowerBound = Guess + 1;
        ++guessCount;
        Guess = numGuess(upperBound , Guess);
        getchar();
        printf("Is this your number %d? " , Guess);
        getchar();
        break;
        case 's':
        upperBound = Guess - 1;
        ++guessCount;
        Guess = numGuess(upperBound , lowerBound);
        getchar();
        printf("Is this your number %d? " , Guess);
        getchar();
        break;
       default:
        break;
        }    
        //if user enters 'e' then the computer guess right,           prints the x value, number of guesses and program ends
        if (choice == 'e'){
         printf("Your number must be %d. I used %d   guesses." , Guess , guessCount);
        }
      } while (choice == 'l' || choice == 's');