if 语句中的错误,需要标量逻辑表达式 [英] error in if-statement, requires scalar logical expression

查看:38
本文介绍了if 语句中的错误,需要标量逻辑表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个子程序中,我尝试创建一个语句,但是只有当我直接输入一个数字时它才会起作用,一旦我用一个变量替换这个数字,它就会给出错误:

Within a subroutine I try to create a statement, however it will only work if I enter a number directly, as soon as I replace the number with a variable, it will give the error:

||Error: IF clause  requires a scalar LOGICAL expression|

在这个例子中,var 是一个介于 0 和 1 之间的实数.

In this example var is a real number between 0 and 1.

    if ( var%type3 < 0.5) then
            test = 1
    end if

其中 type3 组件声明为

    real, dimension(1,1) :: type3

有人可能知道我们做错了什么.因为错误并没有给我们任何线索,说明语句的哪一部分是错误的.

Does someone maybe know what we are doing wrong. Because the error does not give us any clues which part of the statement is wrong.

推荐答案

您尝试将 DIMENSION(1, 1) 类型用作 REAL.

You try to use a DIMENSION(1, 1) type as a REAL.

您应该添加 (1, 1) 以访问包含在 DIMENSION(1, 1) 中的 REAL

You shoul add (1, 1) to access to yout REAL contained in the DIMENSION(1, 1)

使用:

  IF ( var%type3(1, 1) < 0.5 ) THEN
     print *, 'IT WORKS'
  END IF

获取此错误的示例:

MODULE vardef
  TYPE vartype
     REAL :: type3(1, 1)
  END TYPE vartype
END MODULE vardef

PROGRAM test
  USE vardef

  TYPE(vartype) var

  var%type3(1, 1) = 0

  IF ( var%type3 < 0.5 ) THEN
     print *, 'IT WORKS'
  END IF
  RETURN

END PROGRAM test

这篇关于if 语句中的错误,需要标量逻辑表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆