使用"X!= 0"时的语法错误在Fortran中 [英] Syntax error when using "X != 0" in Fortran

查看:225
本文介绍了使用"X!= 0"时的语法错误在Fortran中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Fortran程序有一个问题,该问题只不过是计算素因数分解(或应该这样做)而已.那是错误:

I have a problem with my Fortran program that does nothing more than calculating a prime factorization (or should do). That's the error:

C:\MinGW\Fortran>gfortran aufg3.f90
aufg3.f90:15.15:

    if (prim(i) != 0 .and. modulo(n, prim(i)) == 0) then
               1
Error: Missing ')' in statement at or before (1)
aufg3.f90:19.7:

    end if
       1
Error: Expecting END DO statement at (1)
aufg3.f90:34.13:

  if (prim(i) != 0) then
             1
Error: Missing ')' in statement at or before (1)
aufg3.f90:38.5:

  end if
     1
Error: Expecting END DO statement at (1)

我尝试了一切,但完全不知道可能出什么问题.谢谢你的帮助.这是代码:

I tried everything, but totally have no idea what could be wrong. Thanks for your help. Here is the code:

program aufg3
    implicit none
    integer :: n, i
    integer, allocatable, dimension(:) :: prim
    do
        print *, 'Bitte natürliche Zahl eingeben, "0" für Abbruch: '
        read *, n
        if (n == 0) exit
        allocate(prim(2:n))
        call era(prim, n)
        print *, n, ' = 1'
        do
            if (n == 1) exit
            do i = 2, n
                if (prim(i) != 0 .and. modulo(n, prim(i)) == 0) then
                    print *, ' * ', prim(i)
                    n = n / prim(i)
                    exit
                end if
            end do
        end do
        deallocate(prim)
    end do
end program

subroutine era(prim, m) integer, intent(in) :: m integer, dimension(2:m) :: prim integer :: i, j do i = 2, m prim(i) = i end do do i = 2, integer(sqrt(real(m))) if (prim(i) != 0) then do j = i*i, m, i prim(j) = 0 end do end if end do end subroutine

subroutine era(prim, m) integer, intent(in) :: m integer, dimension(2:m) :: prim integer :: i, j do i = 2, m prim(i) = i end do do i = 2, integer(sqrt(real(m))) if (prim(i) != 0) then do j = i*i, m, i prim(j) = 0 end do end if end do end subroutine

推荐答案

好,这是Fortran,!表示注释.因此,编译器实际上会看到

Well, this is Fortran and ! denotes a comment. So the compiler actually sees

if (prim(i) 

这不是有效的声明.您看到的错误消息反映了这一点.

which is no valid statement. The error message you see reflects that.

Fortran中的不等于"是/=.ne.:

"Not equal" in Fortran is /= or .ne.:

 if (prim(i) /= 0 .and. modulo(n, prim(i)) == 0) then

,随后:

if (prim(i) /= 0) then

这篇关于使用"X!= 0"时的语法错误在Fortran中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆