- UID
- 6121
- 性别
- 男

计算固体力学研发
- 积分
- 25
F 币- 6 元
- 最后登录
- 2022-10-13
贡献- 15 点
- 注册时间
- 2022-10-1
权杖- 0 枚
惯用编译器:Intel Visual Fortran for Windows

新人
计算固体力学研发
F 币- 6 元
贡献- 15 点
|
1F 币
本帖最后由 xiejihong0306 于 2022-10-8 11:36 编辑
彭国论教材EX0829.F90,用递归函数实现阶乘。
可我遇到了下面这个问题,按照粘贴的源代码那样,运行后输入n!,程序没报错,也没输出结果,想了半天实在没看懂哪有问题,
如果可以的话,想请高人解惑,万分感谢

-----------------------------
补充: 不好意思题干描述有点问题,我输入的9 ,没加感叹号
![]()
[Fortran] 纯文本查看 复制代码 08 | integer ( kind = 4 ) , external :: fact |
09 | write ( unit = 6 , fmt = "('N = ')" ) |
10 | read ( unit = 5 , fmt = * ) n |
11 | write ( unit = 6 , fmt = "(I3,'! = ', I8)" ) n , fact ( n ) |
14 | write ( * , * ) 'Press input any words to exit...' |
17 | end program ProgramName |
20 | recursive integer ( kind = 4 ) function fact ( n ) result ( ans ) |
22 | integer ( kind = 4 ) , intent ( in ) :: n |
23 | integer ( kind = 4 ) , save :: count = 1 |
24 | integer ( kind = 4 ) :: localcount , temp |
27 | write ( unit = 6 , fmt = "(I2, 'th enter, n = ', I2)" ) localcount , n |
32 | write ( unit = 6 , fmt = "(I2, 'th exit, n = ', I2, 'ans = ', I8)" ) localcount , n , ans |
34 | else if ( n <= 1 ) then |
36 | write ( unit = 6 , fmt = "(I2, 'th exit, n = ', I2, 'ans = ', I8)" ) localcount , n , ans |
41 | write ( unit = 6 , fmt = "(I2, 'th exit, n = ', I2, 'ans = ', I8)" ) localcount , n , ans |
|
最佳答案
查看完整内容
gfortran的函数里面写输出会发生混乱,ifort应该没这个问题
不要同时输出即可
|