Fortran如果显示全部但未选中

问题描述 投票:-1回答:1

晚上好!我正在尝试使用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"
  if a = 1901
  print *, "First trans-oceanic radio transmission"
  read *
  end program Calc

但是我插入的每个输入都会显示所有输出,而不是选定的输出。例如,如果我输入“ 1900”,它将同时显示1900和1901事实。但这不是我想要的。您知道我该怎么办吗?[Thxxx

fortran
1个回答
1
投票

正如我在对您的问题的评论中所说的那样,在您的情况下,select case构造可能更整洁:

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
© www.soinside.com 2019 - 2024. All rights reserved.