从下面的代码可以看出,当询问您是否继续或停止程序时,按下其他键(如“;”或“,”),它会将其读取为您按下了“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
response
是;
(或几乎任何其他字符,比如n
),则response /= 'Y'
的求值结果是什么。现在考虑response /= 'y'
并思考.and.
的意义。 - High Performance Mark.and. response /='y'
时,它就可以正常工作了? - EnthusiastiC