简单的 IO
作为写入输入和输出的示例,我们将获取实数值并返回值及其平方,直到用户输入负数。
如下所述,read
命令有两个参数:单元号和格式说明符。在下面的示例中,我们使用*
作为单元号(表示 stdin)和*
作为格式(在这种情况下表示 reals 的默认值)。我们还指定了 print
语句的格式。也可以使用 write(*,"The value....")
或者简单地忽略格式化并将其作为
print *,"The entered value was ", x," and its square is ",x*x
这可能会导致一些奇怪的间隔字符串和值。
program SimpleIO
implicit none
integer, parameter::wp = selected_real_kind(15,307)
real(kind=wp) :: x
! we'll loop over until user enters a negative number
print '("Enter a number >= 0 to see its square. Enter a number < 0 to exit.")'
do
! this reads the input as a double-pricision value
read(*,*) x
if (x < 0d0) exit
! print the entered value and it's square
print '("The entered value was ",f12.6,", its square is ",f12.6,".")',x,x*x
end do
print '("Thank you!")'
end program SimpleIO