gpt4 book ai didi

C 程序中无法退出 while 循环

转载 作者:行者123 更新时间:2023-11-30 16:51:03 25 4
gpt4 key购买 nike

我在 C 编程入门类(class)的 C 程序中遇到了 while 循环问题。我需要告诉计算机它的数字猜测是更大还是更小,然后当它正确时,它应该打印数字和猜测的数量。程序进入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;
}

最佳答案

好吧,我修改了你的一些代码,以便它可以满足你的要求。我建议您停止使用变量名称,例如 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;
}

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

关于C 程序中无法退出 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41916943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com