在C语言中如何实现按任意键继续?

Python094

在C语言中如何实现按任意键继续?,第1张

1、直接调用系统函数 system("pause"):

system()会调用fork()产生子进程, 由子进程来调用/bin/sh-c string 来执行参数string 字符串所代表的命令, 此命令执行完后随即返回原调用的进程。

system("pause")可以实现冻结屏幕,便于观察程序的执行结果。如下:

2、调用getch()函数:

此函数是一个不回显函数,当用户按下某个字符时,函数自动读取,无需按回车,需要include<conio.h>,如下:

扩展资料:

如果需要按下Enter 键才行的思路:

1、调用getchar()函数:

从stdio流中读字符,当程序调用getchar时,程序就等着用户按键,用户输入的字符被存放在键盘缓冲区中,直到用户按回车为止。

需要include<stdio.h>,如下:

参考资料:

百度百科--system()

百度百科--getch()

百度百科--getchar()

最简单的形式

#include<stdio.h>

void main()

{

char answer

do

{

//相关内容

//判断是否继续

printf("是否继续? (Y/N)")

fflush(stdin)

scanf("%c",&answer)

}while(answer=='Y')

}

#include

<stdio.h>

#include

<stdlib.h>

#include

<time.h>

int

shuru(int

num)

{

int

a

srand(num*num+time(0))

a=(rand()+num)%10//产生10以内的随机数

return

a

}

void

main()

{

int

i,num,a,b,c,fuhao,error

char

ch='y'

//新添加

error

=

0//错误数置零

num

=

100//使得下面的while循环成立即可

while(ch=='Y'||ch=='y')

新添加

{

while(num>0&&num<10)//只接受10以内的题目数

{

printf("Please

input

the

sum

of

exam

:")

scanf("%d",&num)

}

for(i=0i<numi++)//每次出题开始

{

a

=

shuru(i)//第一个操作数

b

=

shuru(a)//第二个

fuhao

=

(shuru(a+b)+b)/a%2//产生符号

+或者-

switch(fuhao)

{

case

0://+

if(a+b>10)//超过10重新来

{

i--

break

}

else

{

printf("%d

:

%d

+

%d

=

",i+1,a,b)//出题

scanf("%d",&c)//取输入的结果

if(c

!=

a+b)//答案错误

{

printf("Error!\n")

error++

}

else//正确答案鼓励一下

{

printf("Good!\n")

}

}

break

case

1://-

if(b>a)//保证a>b

使得不出现负数

{

a=a+b

b=a-b

a=a-b

}

printf("%d

:

%d

-

%d

=

",i+1,a,b)//出题

scanf("%d",&c)//取结果

if(c

!=

a-b)

{

printf("Error!\n")

error++//错误自加

}

else

{

printf("Good!\n")

}

break

default:

break

}

}

printf("%d

errors!\n",error)//输出错误数

printf("分数:

%d%\n",

(num-error)*100/num)

printf("是否继续

Y\N\n")

}

}