zlzshubao 发表于 2015-5-16 20:30:15

新手求教:关于数组的一个问题

!输入十个人的学号和成绩,高于平均分的学号和成绩打印出来
program yatou
implicit none

integer b(11)
integer i,o,p,q,r,s,t
o=0
p=0
q=0
r=0
s=0
do 10,i=1,10,1
   read*,b(i)
10 continue

do 20,i=1,10,1
   if(60>b(i)>0) then
   o=o+1
   endif
   if(59<b(i)<70) then
      p=p+1
   endif
   if(69<b(i)<80) then
   q=q+1
   endif
   if(79<b(i)<90) then
   r=r+1
   endif
   if(89<b(i)<100) then
   s=s+1
   endif
20 continue

print*,"不及格人数",o
print*,"60——69",p
print*,"70——79",q
print*,"80——89",r
print*,"90——100",s
end
不知道为什么,总是显示

楚香饭 发表于 2015-5-16 20:58:40

最重要的是 Fortran 不支持连续判断 a<b<c,必须写成 a<b.and.b<c
另外,也给了一些其他的修改意见。

Program yatou
Implicit None
Integer :: b(10) !10个就够了
Integer :: i, o = 0, p = 0, q = 0, r = 0, s = 0 !建议定义时给初值
do i=1,10
    Read(*,*) b(i)
end do!建议用 End Do
!b = [ 68, 44, 5, 5, 5, 5, 6, 6, 7, 78 ]

Do i = 1, 10
    If (60>b(i) .And. b(i)>=0) Then !//不能连续判断
      o = o + 1
    End If
    If (59<b(i) .And. b(i)<70) Then
      p = p + 1
    End If
    If (69<b(i) .And. b(i)<80) Then
      q = q + 1
    End If
    If (79<b(i) .And. b(i)<90) Then
      r = r + 1
    End If
    If (89<b(i) .And. b(i)<=100) Then !最好是小于等于100
      s = s + 1
    End If
End Do!建议用 End Do
Print *, '不及格人数', o
Print *, '60——69', p
Print *, '70——79', q
Print *, '80——89', r
Print *, '90——100', s
End Program yatou

百事可乐 发表于 2015-5-17 10:18:24

用 count函数不是很方便么?

Program yatou
Implicit None
Integer :: b(10) !10个就够了
Integer :: i, o = 0, p = 0, q = 0, r = 0, s = 0 !建议定义时给初值
do i=1,10
    Read(*,*) b(i)
end do!建议用 End Do
!b = [ 68, 44, 5, 5, 5, 5, 6, 6, 7, 78 ]
o = count( b<60.and.b>=0 )
Print *, '不及格人数', o
p = count( b<70.and.b>=60 )
Print *, '60——69', p
q = count( b<80.and.b>=70 )
Print *, '70——79', q
r = count( b<90.and.b>=80 )
Print *, '80——89', r
s = count( b<=100.and.b>=90 )
Print *, '90——100', s
End Program yatou

zlzshubao 发表于 2015-6-1 21:38:04

楚香饭 发表于 2015-5-16 20:58
最重要的是 Fortran 不支持连续判断 a

超级感谢!!麻烦你啦~

zlzshubao 发表于 2015-6-1 21:38:25

zlzshubao 发表于 2015-6-1 21:38
超级感谢!!麻烦你啦~

哈哈,棒!谢谢~
页: [1]
查看完整版本: 新手求教:关于数组的一个问题