Fortran if显示全部和未选择
晚上好!我正试图用Force 2.0在fortran中创建一个虚拟时间轴 我想做一个if,接受“a”值,即年份,并用if显示当年发生的事实 我写了这段代码:Fortran if显示全部和未选择,fortran,Fortran,晚上好!我正试图用Force 2.0在fortran中创建一个虚拟时间轴 我想做一个if,接受“a”值,即年份,并用if显示当年发生的事实 我写了这段代码: program Calc real :: a print *, "Inserisci la data" print *, "Type the first number: " read *, a if a = 1900 print *, "London arrives to 4 milion inhabitants
program Calc
real :: a
print *, "Inserisci la data"
print *, "Type the first number: "
read *, a
if a = 1900
print *, "London arrives to 4 milion inhabitants"
if a = 1901
print *, "First trans-oceanic radio transmission"
read *
end program Calc
但我插入的每个输入都会显示所有输出,而不是选定的输出。
例如,如果我输入“1900”,它会显示1900和1901事实”,但这不是我想要的。你知道我能做什么吗?正如我在对你的问题的评论中所说的那样,
选择案例
结构在你的案例中可能更整洁:
program test
implicit none
integer:: year
write(*,*) "Type the first number: "
read(*,*) year
select case(year)
case(:1899)
write(*,*) "I guess something happened before 1900"
case(1900)
write(*,*) "I am sure at least one cat was born in 1900"
case(1901:1905)
write(*,*) "I am sure something happened between 1901 and 1905"
case(1906:)
write(*,*) "Everything past 1906"
case default
write(*,*) "default case"
end select
read(*,*)
end program test
。请注意,我已将年份类型更改为整数。添加
隐式无
作为第二行,以查看错误。这不是if
语句的工作方式。在固定格式源中,如果a=1900,则语句将值1900赋给变量ifa
。请参见示例。if(a==1900)…
以此类推。添加双等号和括号后,会显示以下内容:C:\Users\Giulio\Desktop\~VTM.f:6.72:if(a==1900)1错误:无法在(1)处指定命名常量C:\Users\Giulio\Desktop\~VTM.f:8.72:我想在你的情况下,构造会更整洁。那个文档真的很复杂!你能给我一个变量的例子吗?