此代码是否算作Fortran的错误?

问题描述 投票:0回答:1

您可以从下面的代码中看到,当它询问您是继续还是停止该程序时,请按其他键,例如“;”。或“,”会在您单击“是/否”按钮时将其读取,但实际上并非如此。所以,我问这是错误还是滥用代码?

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 gfortran
1个回答
0
投票

我在Intel Fortran中尝试了此代码,它可以按预期工作。当responseyY时,它将循环播放,否则退出循环。

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)
    print '(a,2f10.3)', 'The difference in volumes is, ',abs(vol1-vol2)
    print *, 'Any more? - hit Y for yes, otherwise hit any key'
    read *, response
    if (response /= 'Y' .and. response /= 'y') stop
end do

contains

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

end program vols

PS。我将函数移到了program块中,因此它不需要接口或外部声明。

© www.soinside.com 2019 - 2024. All rights reserved.