C语言:猜数问题!!
注意:为了能够用系统进行评判,提交的程序需要把随机数生成部分替换为从标准输入(stdin)读取一个数(这个数相当于随机数);然后,后面继续读取用户输入的数。 展开
#include <stdio.h>
int main()
{
int tar, guess, times, again = 1;
char ch;
while (again == 1)
{
scanf("%d", &tar);
times = 1;
while (1)
{
scanf("%d", &guess);
if (guess == tar)
{
if (times == 1)
printf("Bingo!\n");
else if (times <= 3)
printf("Lucky You!\n");
else
printf("Good Guess!\n");
break;
}
if (times >= 6)
{
printf("Game Over.\n");
break;
}
if (guess > tar)
printf("Too big.\nTry again:");
else
printf("Too small.\nTry again:");
times++;
}
again = 0;
do
{
printf("Do you wanna play again? "
"If yes, press Y or y. Otherwise press N or n.");
scanf(" %c", &ch);
if (ch == 'Y' || ch == 'y')
again = 1;
else if (ch == 'N' || ch == 'n')
again = -1;
} while (again == 0);
}
return 0;
}
2020-11-28 · 百度认证:云南新华电脑职业培训学校官方账号
#include<stdio.h>
void main()
{
int i,j,A,B,n=0;
int a[4]={1,2,3,4},b[4];
do{
A=0;
B=0;
printf("please imput 4 numbers from 1 to 10:"); //我用的VC6.0,数据输入之间空格;
for(i=0;i<4;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
if(b[j]==a[i]) //等号==;
{
i!=j?B++:A++;
}
}
n++;
printf("%dA%dB\n",A,B);
}while(A<4);
printf("Congratulations!!!\n");
printf("You have guessed %d times in all\n",n);
printf("The answer is:");
for(i=0;i<4;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
你的循环有点,局部还有些错误,我这个可以了,不懂的在线HI非常感谢您的耐心观看,如有帮助请采纳,祝生活愉快!谢谢!