Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 我应该在程序中的何处放置打开的文件指示器?_Fortran - Fatal编程技术网

Fortran 我应该在程序中的何处放置打开的文件指示器?

Fortran 我应该在程序中的何处放置打开的文件指示器?,fortran,Fortran,现在,这模拟了一个三维随机行走,它被调整为有50个粒子。它没有在系统中运行,所以我只是想知道它有什么问题。有人有线索吗 program RW3D implicit none open (1, file = ‘sarathi.txt’) integer, parameter : : n = 50 integer : : x(50), y(50), z(50) integer : : i, j real : : P x = 0

现在,这模拟了一个三维随机行走,它被调整为有50个粒子。它没有在系统中运行,所以我只是想知道它有什么问题。有人有线索吗

program RW3D

    implicit none

    open (1, file = ‘sarathi.txt’)

    integer, parameter : : n = 50
    integer : : x(50), y(50), z(50)
    integer : : i, j
    real : : P

    x = 0
    y = 0
    z = 0

    do i = 1, 100
    do j = 1, 50
       call random_number (p)

        write (1,*) i, x, y, z

         if (p .lt. 1.0/6) then
            x(j) = x(j) - 1
         else if (p .lt. 2.0/6) then
            y(j) = y(j) - 1
         else if (p .lt. 3.0/6) then
            z(j) = z(j) - 1
         else if (p .lt. 4.0/6) then
            x(j) = x(j) + 1
         else if (p .lt. 5.0/6) then
            y(j) = y(j) + 1
         else
            z(j) = z(j) + 1
        end if

    end do

end program RW3D

OPEN
语句不能在声明之前。在最后一次声明之后移动它

还要注意
::
,它应该是
::

正如在一篇评论中所说,这里缺少了一个
end do

“这是我的代码,它不起作用,请修复它”在这里不是很受欢迎,正如你的分数所示

“它没有运行”是非常模糊的。更好的描述是:

“无法编译,并显示错误消息:

RW3D.f90:7.12:

    integer, parameter : : n = 50
            1
Error: Invalid character in name at (1)
我做错了什么?”

您的代码有很多错误:

  • 在声明块(所有
    整数
    实数
    声明)完成之前的可执行语句(
    open(1,file='sarathi.txt')

  • 文件名不是用正确的
    打开的——可能是您的文本编辑器用外观更好的单引号替换了该文件名,但单引号不起作用

  • 两个冒号之间不应该有空格

  • 每个do循环都需要自己的
    end do

  • 请不要使用小于10的文件i/o单元号。这是自找麻烦。更好的是,使用
    newunit
    关键字:

  • 下面是
    newunit

    program hello
        implicit none
        integer :: my_unit
        open(newunit=my_unit, file='delme.txt', action='write')
        write(my_unit, *) "Hello World"
        close(my_unit)
    end program hello
    
    试试这些,你会走得更远