Arrays 如何在Fortran中设置一个小代码复活节彩蛋-随机选择错误消息的引号

Arrays 如何在Fortran中设置一个小代码复活节彩蛋-随机选择错误消息的引号,arrays,random,fortran,character,Arrays,Random,Fortran,Character,我创建了一个程序,它有一个用户界面,可以从菜单中选择一个选项。此选项被馈送到if语句,该语句将其重定向到正确的子例程。我还设置了一个小错误消息,以防选项输入错误 else write(*,*)'' write(*,*)'' write(*,*)'' write(*,*)'I am sorry, Dave. I am afraid I cannot do that!' write(*,*)''

我创建了一个程序,它有一个用户界面,可以从菜单中选择一个选项。此选项被馈送到if语句,该语句将其重定向到正确的子例程。我还设置了一个小错误消息,以防选项输入错误

        else
        write(*,*)''
        write(*,*)''
        write(*,*)''
        write(*,*)'I am sorry, Dave. I am afraid I cannot do that!' 
        write(*,*)''
        write(*,*)''
        write(*,*)''
        write(*,*)' Press enter to return to main menu! '
        read(*,*)
        goto 300
    end if 
一切都很好。事实是-HAL9000有很多标志性短语,它们可能会像错误消息一样棒,因此我想改进这个
if block
,为用户提供从一组预定义短语中随机选择的短语。这些短语的示例:

    'This mission is too important for me to allow you to jeopardize it.'
    'I know I've made some very poor decisions recently, but I can give you my complete assurance that my work will be back to normal.'
    'I've still got the greatest enthusiasm and confidence in the mission. And I want to help you.'
    'Just what do you think you're doing, Dave?'
    'Look Dave, I can see you're really upset about this.'
    'I honestly think you ought to sit down calmly, take a stress pill, and think things over.'
好吧,起初我认为这很简单:只需构建一个字符数组,弹出其中的短语,然后执行以下操作:

program pick_random
  implicit none
 
  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r
 
  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
但很明显,Fortran似乎不是这样工作的,字符数组显然有一个我所希望的

关于如何实现它,有什么建议吗?

在对我的源代码中缺少的概念进行了宝贵的评论之后,我成功地创建了代码,并且工作得很好!为了将来的参考,我会把它贴在这里

  subroutine easter_eggs()
    implicit none
    character(len=100),dimension(8) :: easter_egg
    integer :: i
    real :: r
   


easter_egg=(/&
        &'Are you from the future? Because I have not written such option yet! Wierd...!                      ',&
        &'Mistakes allow thinking to happen!                                                                  ',&
        &'Let me state the obvious: Are you sure you type the keyword correctly?                              ',&
        &'There are no easter eggs in this program. Yep! That is right! None! And you there is no such option!',&
        &'You have learned so much from your mistakes that you decided to make a few more, right?             ',&
        &'Just what do you think you are doing, Dave?                                                         ',&
        &'I see you never do the same mistake twice - you like to do it often just to be sure!                ',&
        &'Anyone who has never made a mistake has never tried anything new! (Albert Einstein)                 '/)
    
    call random_seed
    call random_number(r)
    write(*,*)easter_egg(int(r*size(easter_egg))+1)
    
    end subroutine easter_eggs

跟进:根据@francescalus的建议,我尝试将字符数组重写如下



它编译得很好,但每次我输入一个错误的关键字时,只会弹出一个短语——与字符数组中设置的第一个短语对应的短语

您能用字符数组显示您的尝试吗?如果我这样做的话,这正是我希望使用的。可能参考文章中缺少的必要概念是,可以定义特定长度的字符变量数组,例如
character(len=32),dimension(24)::random_strings
。正如我们所说的,Bob是你母亲的兄弟。字符数组构造函数有一个很好的例子,它可以避免用空格进行痛苦的填充。请注意,上面使用的
调用random\u seed
可能有问题。Fortran标准没有定义此调用是使用一组新的种子为PRNG设定种子,还是使用一组处理器相关的种子为PRNG重新设定种子。因此,
r
可能总是具有相同的值。最好使用
random\u init
。它完全跳过了我!而且,奇怪的是编译器怎么没有标记它!使用
random_init(.true.,…)
可以请求一个可重复的流。这意味着你每次都得到相同的号码。使用
.false.
获取不同的(不可重复的)数字。
   subroutine easter_eggs()
    implicit none      
    integer :: i
    real :: r
    character(len=100),dimension(8) :: easter_egg = [ character(len=100) :: &
    &"Are you from the future? Because I have not written such option yet! Wierd...!",&
    &"Mistakes allow thinking to happen!",&
    &"Let me state the obvious: Are you sure you type the keyword correctly?",&
    &"There are no easter eggs in this program. Yep! That is right! None! And you there is no such option!",&
    &"You have learned so much from your mistakes that you decided to make a few more, right?",&
    &"Just what do you think you are doing, Dave?",&
    &"I see you never do the same mistake twice - you like to do it often just to be sure!",&
    &"Anyone who has never made a mistake has never tried anything new! (Albert Einstein)"]

    call random_init(.true., .true.)
    call random_number(r)
    write(*,*)easter_egg(int(r*size(easter_egg))+1)

    end subroutine easter_eggs

    end module formalism