如何在一个循环中打印一行? [英] How to print in a single row in a loop?
问题描述
假设我有一个包含以下循环的Fortran程序:
do i = 1,10
print *,i
enddo
这个的输出就像
1
2
...
10
如何将这些值写入单行,如下所示?
1 2 ... 10
有很多方法,其中有两个立即出现在下面的小程序中:
$ cat loop.f90
程序循环
隐含无
整数:: i
写入(*,*)'第一种方式 - 非前进I / O'
Do i = 1,10
写入(*,'(i0,1x)',高级='否')i
完成
写入(*,*)!完成记录
写入(*,*)'第二种方式 - 暗指做循环'
写入(*,*)(i,i = 1,10)
结束程序循环
$ gfortran -std = f2003 -Wall -Wextra -fcheck = all loop.f90
$ ./a.out
第一种方式 - 不进行I / O
1 2 3 4 5 6 7 8 9 10
第二种方式 - 暗示做循环
1 2 3 4 5 6 7 8 9 10
$
第一种方法。非前进I / O,抑制正在写入的记录标记的结束,这通常是新行,但确实需要明确的格式。第二个暗示的do循环不需要格式,但不够灵活。
BTW英文版通常称为循环
Suppose I have a Fortran program which includes the following loop:
do i=1, 10
print *, i
enddo
The output of this will be like
1
2
...
10
How can I write these values to a single line, like in the following?
1 2 ... 10
There are a number of ways, two that come to mind immediately are shown in the following little program
$ cat loop.f90
Program loop
Implicit None
Integer :: i
Write( *, * ) 'First way - non-advancing I/O'
Do i = 1, 10
Write( *, '( i0, 1x )', Advance = 'No' ) i
End Do
Write( *, * ) ! Finish record
Write( *, * ) 'Second way - implied do loop'
Write( *, * ) ( i, i = 1, 10 )
End Program loop
$ gfortran -std=f2003 -Wall -Wextra -fcheck=all loop.f90
$ ./a.out
First way - non-advancing I/O
1 2 3 4 5 6 7 8 9 10
Second way - implied do loop
1 2 3 4 5 6 7 8 9 10
$
The first method. non-advancing I/O, suppresses the end of record marker being written, which is normally a new line, but does require an explicit format. The second, implied do loop, doesn't require a format, but is less flexible.
BTW in English they are normally called "loops"
这篇关于如何在一个循环中打印一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!