‘壹’ C语言编写猜数字游戏
不用把你的作业题目一字不差的贴出来吧
‘贰’ 急求 用C语言编写猜数游戏程序设计及其流程图(传统流程图或结构化流程图)和程序代码(关键部分加注释)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
voidmain()
{
intran;
intn;
intcount=0;
srand((unsigned)time(NULL));
ran=rand()%100+1;
while(1)
{
printf("PleaseInput: ");
scanf("%d",&n);
if(n>100||n<1)
{
printf("Inputerror ");
return;
}
if(n<ran)
{
printf("Inputmin ");
count++;
continue;
}elseif(n>ran)
{
printf("Inputmax ");
count++;
continue;
}else
{
printf("conguratulation! ");
count++;
break;
}
}
if(count<3)
printf("Verygood ");
elseif(count>=3&&count<=5)
printf("Good ");
else
printf("Youcandobetter ");
}
‘叁’ C++语言编程:猜数游戏.
考虑到主函数中调用了guess()和guess(int)两个函数。故将guess重载。
int guess()
{
return 50;
}
int guess(int ans)
{
static int min=0,max=100;
if(ans==1)
max=(max+min)/2;
else
min=(max+min)/2;
return (max+min)/2;
}
‘肆’ C语言编程 猜数游戏
#include
#include
//用到了rand函数,所以要有这个头文件
#include
//用到了time函数,所以要有这个头文件
int
main()
{
int
number;
//number用于存储随机数
int
guess=0;
//guess用于存储玩家猜的数
srand((unsigned)
time(null));//用系统时间作为rand函数使用的种子
number=rand()%100;
//随机除以100,取余数
number++;
//余数加1
printf("猜数字游戏\n");
printf("该数字在1到100之间\n");
while(guess!=number)
{
printf("请输入您所猜的数:");
scanf("%d",&guess);
//如果玩家猜的数较小,给予提示
if
(guess
number)
{
printf("大了\n");
}
}
//猜中则循环结束,输出猜中的数字
printf("猜对了,这个数字就是:%d\n",number);
return
0;
}
‘伍’ 猜数游戏C语言程序设计
这道题不难,只要知道怎样用c语言生成1~100的随机数就很好办了!
附代码如下!
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
intmain()
{
index:
printf("请选择是否进行猜数! 1:是 0:否 ");
intj;
scanf("%d",&j);
if(j==0)
return0;
inti,x=rand()%100+1,input;
for(i=0;i<10;i++)
{
printf("请输入猜想的1至100之间的整数:");
scanf("%d",&input);
if(input==x)
{
printf("猜想正确! 你一共猜想了%d次 ",i+1);
gotoindex;
}
if(input<x)
{
if((i+1)>=10)
{
printf(" 此次猜想失败! ");
gotoindex;
}
else
printf("所猜数过小! 请再猜一次! ");
}
if(input>x)
{
if((i+1)>=10)
{
printf(" 此次猜想失败! ");
gotoindex;
}
else
printf("所猜数过大! 请再猜一次! ");
}
}
}
为了简化,使用了goto语句。
‘陆’ 如何用C语言编辑猜数游戏, 要用循环结构程序设计
#include<stdio.h>
intmain()
{
floatx;
for(inti=0;i<5;i++)
{
printf("请输入一个数字:");
scanf("%f",&x);
if(x>12)printf("你输入的数字太大: ");
if(x<12)printf("你输入的数字太小: ");
if(x==12){printf("恭喜你! ");break;}
}
//getchar();
//getchar();
}
运行结果如下图:有问题追问
‘柒’ C语言编程:编写一个猜数的游戏,系统自动产生一个随机数,你来猜,程序给出提示,直到猜对为止。
import java.util.*;
class Assignment8{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int x=(int)(Math.random()*100);//生成一个0~100的随机数
int y=-1;
System.out.println("已生成0~100的随机整数,请输入您所猜的数:");
while(x!=y)
{
y=sc.nextInt();
if(y>x)
{
System.out.println("输入的数过大");
}
else if(y<x)
{
System.out.println("输入的数过小");
}
}
System.out.println("正确!该随机数是"+x);
sc.close();
}
}
while循环的格式:while(表达式){语句;}
while循环的执行顺序:当表达式为真,则执行下面的语句,语句执行完之后再判断表达式是否为真,如果为真,再次执行下面的语句,然后再判断表达式是否为真……就这样一直循环下去,直到表达式为假,跳出循环。
例:
int a=NULL;
while(a<10){
a++;//自加
if(a>5)//不等while退出循环,直接判断循环
{break;//跳出循环}
}
结果:结束后a的值为6。
‘捌’ 猜数游戏。在程序中预设一个0~9之间的数,让用户通过键盘输入所猜的数,写出代码及画出该过程的流程图
‘玖’ 如何猜数游戏C语言程序设计
编程如下:
#include
#include
#include
int main(int argc, char * argv[])
{
srand((unsigned)time(NULL));
int a = rand()%100;
a+=1;
int b,c;
c = 0;
while (c {
printf("Please enter a number: ");
if (scanf("%d", &b) == 0)
{
fprintf(stderr, "Invalid entry. ");
return EXIT_FAILURE;
}
else
{
if (b == a)
{
printf("You are correct, the number is %d! ", a);
printf("You used %d times to get the answer. ", c);
break;
}
else if (b >a)
printf("The number is bigger than it suppose to. ");
else
printf("The number is smaller than it suppose to. ");
c++;
}
if (c == 10)
{
printf("You used 10 times, please try again! ");
break;
}
}
return EXIT_SUCCESS;
}
‘拾’ java猜数字的流程图
/* 1程序运行时自动产生1-100 随机数
* 接受用户键盘输入数据并给出指示信息
* 直至猜对,游戏者猜对后,显示猜对次数
* 并提供“重新开始”与“退出”功能(此处最重要)。
*/
import java.util.Random;
import java.util.Scanner;
public class Wocaicaicai
{
public static int getInputNum()
{
int inputNum=-1;
Scanner sc=new Scanner(System.in);
System.out.println("请猜题...");
try
{
inputNum=sc.nextInt();
}
catch(Exception e)
{
inputNum=-1;
}
return inputNum;
}
public static int game()
{
int cishu=0;
int result=0;
Random rdm=new Random();
result=rdm.nextInt(100)+1;
while(true)
{
int i=getInputNum();
if(i<result)
{
System.out.println("低了!");
cishu++;
}
if(i>result)
{
System.out.println("高了!");
cishu++;
}
if(i==result)
{
System.out.println("回答正确!");
cishu++;
break;
}
}
return cishu;
}
public static Boolean IsFirstStart(boolean bol)
{
boolean IsStart=false;
while(true)
{
System.out.println("———————————————————————————————");
System.out.print("请选择: ");
if(bol)
System.out.println("1.开始游戏");
else
System.out.println("1.重新游戏");
System.out.println(" 2.退出");
System.out.println("———————————————————————————————");
Scanner sc=new Scanner(System.in);
try
{
switch(sc.nextInt())
{
case 1:
IsStart=true;
break;
case 2:
System.out.println("--谢谢您的参与--");
System.exit(0);
}
}
catch(Exception e){}
if(IsStart)
break;
}
return IsStart;
}
public static void main(String[] args)
{
boolean isFirst=true;
while(true)
{
if(isFirst){
if(IsFirstStart(true))
{
System.out.println("您猜了 "+game()+" 次.");
isFirst=false;
}
}
else if(IsFirstStart(false))
System.out.println("您猜了 "+game()+" 次.");
}
}
}