【发布时间】:2020-02-12 13:24:26
【问题描述】:
从下面的代码可以看出,当它询问你是继续还是停止程序时,通过按“;”等其他键或“,”它读起来就好像您按下了“Y”或“y”键但您没有按下。所以,我在问这是编译器中的错误,还是代码有问题?
program vols
!Calculates difference in volume of 2 spheres
implicit none
real :: rad1,rad2,vol1,vol2
character :: response
do
print *, 'Please enter the two radii'
read *, rad1,rad2
call volume(rad1,vol1)
call volume(rad2,vol2)
write(*,10) 'The difference in volumes is, ',abs(vol1-vol2)
10 format(a,2f10.3)
print *, 'Any more? - hit Y for yes, otherwise hit any key'
read *, response
if (response /= 'Y' .and. response /= 'y') stop
end do
end program vols
!________________________________________________
subroutine volume(rad,vol)
implicit none
real :: rad,vol,pi
!calculates the volume of a sphere
pi=4.0*atan(1.0)
vol=4./3.*pi*rad*rad*rad
!It's a little quicker in processing to do r*r*r than r**3!
end subroutine volume
【问题讨论】:
-
欢迎,Fortran 问题请使用标签fortran。
-
如果
response是;(或几乎其他任何东西,试试n),考虑response /= 'Y'的结果。现在考虑response /= 'y'并考虑.and.。 -
@HighPerformanceMark 那么,为什么当我们省略
.and. response /='y'时它可以正常工作? -
第一次输入'Y'或'y',变量“response”的值为'Y'或'y',第二次输入','运行,响应的输入为 NULL,因此通过 LIST-DIRECTED 解释,变量“响应”未更改,因此它保持值“Y”/“y”。英特尔编译器还会循环“,”。 ';'的情况,我不知道,可能是gfortran的bug。