Fortran:IF 语句不起作用(尽管变量具有正确的值)
Fortran: IF statement not working (although variables have correct value)
read(100,5) temp, a, charac
5 format(A11,i1,A7)
read(100,*) b
read(100,*) c
read(100,*) d
if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
.or. d .ne. 0) .and. trim(charac) .ne. 'spline') then
print *, 'Scenario A'
else if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
.or. d .ne. 0) .and. trim(charac) .eq. 'spline') then
print *, 'Scenario B'
else
print *, 'Scenario C'
end if
输入文件:
*------------------------------------------------ --------------
My input file from which i am reading these looks as below:
1 spline
0
0
0
*----------------------------------------------------------------
我想要的
scenario A is triggered if any of the integers a,b,c,d have a value of "1" and input file not containing the word "spline"
scenario B is triggered if any of the integers a,b,c,d have a value of "1" and input file contains the word "spline"
scenario C is triggered if all of the integers a,b,c,d have a value of "0" and input file not containing the word "spline"
*---------------------------------------- ------------------------
我最初是用一些其他词代替场景 A 触发的样条,但我要求场景 A 和 C 在没有写入任何内容时触发,即空 space。
这就是我在 read 中使用格式化的原因,否则当 read 语句在该特定行中找不到任何字符时会转到下一行。
我已经尝试打印变量并且变量显示正确的值来触发场景 B 但它仍然触发场景 A
read(100,*,end=5)a,charac
5 go to 6
6 read(100,*)b
read(100,*)c
read(100,*)d
if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
.or. d .ne. 0) .and. trim(charac) .ne. 'spline') then
print*, 'Scenario A'
else if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
.or. d .ne. 0) .and. trim(charac) .eq. 'spline') then
print*, 'Scenario B'
else
print*, 'Scenario C'
endif
说明:
我面临的问题是我希望代码能够从同一行读取整数 "a" 和字符 "charac" 的值。如果 "charac" 在输入文件中具有特定值,并且如果 "charac" 没有输入,即输入行仅包含整数而没有字符,我希望代码经历不同的场景。
出了问题的是,如果输入文件在该特定行中没有字符,则读取语句会继续读入下一行。
为了避免这种情况,我在读取一定数量的字符后使用格式化来停止读取行,这就是我的问题中显示的内容。这样做 if 语句不能正常工作,可能是由于格式化导致输入没有被正确读取。
我通过在 read 命令中使用 end 语句并使用 go to 命令以另一种方式做到了这一点。所以现在读取语句在该行结束,如果代码没有找到 charac 的任何输入,我会要求代码转到下一个读取语句。
这正是我使用的代码:
read(100,*)curv,spanwise_spline
if (trim(spanwise_spline).eq.'spline')then
go to 6
elseif (trim(spanwise_spline).ne.'spline')then
go to 7
endif
6 read(100,*)
7 read(100,*)thick_distr
read(100,*)
read(100,*)thick
read(100,*)
read(100,*)LE
read(100,5) temp, a, charac
5 format(A11,i1,A7)
read(100,*) b
read(100,*) c
read(100,*) d
if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
.or. d .ne. 0) .and. trim(charac) .ne. 'spline') then
print *, 'Scenario A'
else if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
.or. d .ne. 0) .and. trim(charac) .eq. 'spline') then
print *, 'Scenario B'
else
print *, 'Scenario C'
end if
输入文件: *------------------------------------------------ --------------
My input file from which i am reading these looks as below:
1 spline
0
0
0
*----------------------------------------------------------------
我想要的
scenario A is triggered if any of the integers a,b,c,d have a value of "1" and input file not containing the word "spline"
scenario B is triggered if any of the integers a,b,c,d have a value of "1" and input file contains the word "spline"
scenario C is triggered if all of the integers a,b,c,d have a value of "0" and input file not containing the word "spline"
*---------------------------------------- ------------------------
我最初是用一些其他词代替场景 A 触发的样条,但我要求场景 A 和 C 在没有写入任何内容时触发,即空 space。
这就是我在 read 中使用格式化的原因,否则当 read 语句在该特定行中找不到任何字符时会转到下一行。
我已经尝试打印变量并且变量显示正确的值来触发场景 B 但它仍然触发场景 A
read(100,*,end=5)a,charac
5 go to 6
6 read(100,*)b
read(100,*)c
read(100,*)d
if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
.or. d .ne. 0) .and. trim(charac) .ne. 'spline') then
print*, 'Scenario A'
else if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
.or. d .ne. 0) .and. trim(charac) .eq. 'spline') then
print*, 'Scenario B'
else
print*, 'Scenario C'
endif
说明: 我面临的问题是我希望代码能够从同一行读取整数 "a" 和字符 "charac" 的值。如果 "charac" 在输入文件中具有特定值,并且如果 "charac" 没有输入,即输入行仅包含整数而没有字符,我希望代码经历不同的场景。 出了问题的是,如果输入文件在该特定行中没有字符,则读取语句会继续读入下一行。
为了避免这种情况,我在读取一定数量的字符后使用格式化来停止读取行,这就是我的问题中显示的内容。这样做 if 语句不能正常工作,可能是由于格式化导致输入没有被正确读取。
我通过在 read 命令中使用 end 语句并使用 go to 命令以另一种方式做到了这一点。所以现在读取语句在该行结束,如果代码没有找到 charac 的任何输入,我会要求代码转到下一个读取语句。
这正是我使用的代码:
read(100,*)curv,spanwise_spline
if (trim(spanwise_spline).eq.'spline')then
go to 6
elseif (trim(spanwise_spline).ne.'spline')then
go to 7
endif
6 read(100,*)
7 read(100,*)thick_distr
read(100,*)
read(100,*)thick
read(100,*)
read(100,*)LE