标签: conditional-statements

如何检查 ruby​​ 中的 var 是否为 NaN?

如果数字是“NaN”值,我如何检查 if 语句?我以为这会奏效

2.4.0 :003 >   i = Float::NAN
 => NaN
2.4.0 :004 > puts "hi" if i
hi
 => nil
2.4.0 :005 > puts "hi" if i != Float::NAN
hi
 => nil
2.4.0 :006 > i
 => NaN
2.4.0 :007 > puts "hi" if i == Float::NAN
 => nil
Run Code Online (Sandbox Code Playgroud)

但是正如你所看到的,尽管我将“i”设置为“NaN”,但我得到了上面令人困惑的输出。

ruby floating-point nan conditional-statements

1
推荐指数
1
解决办法
693
查看次数

How can I create a table with conditionally editable input/cells using React?

I have a table like this:

在此处输入图片说明

When a user clicks on an Edit button, an <input> should appear in its place.

If a user clicks on another Edit button, this one will also be replaced with an <input>, and the previous <input> should disappear and show an Edit button again.

In short, only one field can be in edit mode at a time.

This is my initial state:

state = {
    editnameEnable: false,
    editemailEnable: false,
    editaddressEnable: false,
    edittelephone_noEnable: …
Run Code Online (Sandbox Code Playgroud)

javascript html-table conditional-statements reactjs react-state-management

1
推荐指数
1
解决办法
1719
查看次数

YAML 中的条件 (if, else, else if)

我使用 hautelook/AliceBundle 在 YAML 中创建伪造数据,但我希望数据更加一致。

我想这样:

gender: <randomElement(['Homme', 'Femme'])>
if $gender == 'Homme'
    title: 'Monsieur'
else if $gender == 'Femme'
    title: 'Madame'
Run Code Online (Sandbox Code Playgroud)

我知道在 YAML 中不能直接使用,但我不知道要使用哪个插件......以及如何使用。

我的项目中使用的工具/语言(Symfony、hautelook/AliceBundle、PHP、YAML)

conditional-statements nelmio-alice

1
推荐指数
1
解决办法
8052
查看次数

如何每 x 金额检查一个数字

我需要为每个 2016 年创建的文档运行的云函数编写一个条件语句。

所以我有一个变量,每次创建新文档时都会对其进行迭代。在我看来,我认为我可以使用这个变量来检查每个 x 数量。

当前的数量documentsCreated只是一个随机数,而不是一个集合变量。

const documentsCreated = 19239123;

function checkDocuments(){
    let x = (documentsCreated / 2016) % 2016;
    if(x === 2016){
      return true
    } else {
      return false
    }
}
Run Code Online (Sandbox Code Playgroud)

true每次documentsCreated是 2016 的倍数时,此函数都应返回。

我很想只用一个变量来做到这一点,但我想我可能必须保留第二个变量,每次到 2016 年时我都会将其重置为 0。

javascript algorithm conditional-statements

1
推荐指数
1
解决办法
72
查看次数

如何根据前一行过滤行并使用 dplyr 保留前一行?

我正在尝试使用基于前一行的条件对数据集的行进行子集化,同时将前一行保留在子集化数据中。这与此处的问题基本相同,但我正在寻找一种 dplyr 方法:

根据上一行值选择特定行(在同一列中)

我已经采用了对该答案的评论中应用的 dplyr 方法,但我无法弄清楚保留前一行的最后一步。

我可以获得支持我感兴趣的条件的行(incorrect当前一行不是时enter)。

set.seed(123)
x=c("enter","incorrect","enter","correct","incorrect",
"enter","correct","enter","incorrect")
y=c(runif(9, 5.0, 7.5))
z=data.frame(x,y)

filter(z, x=="incorrect" & lag(x)!="enter")
Run Code Online (Sandbox Code Playgroud)

正如预期的那样:

      x        y
1 incorrect 7.351168 
Run Code Online (Sandbox Code Playgroud)

我想要生成的是这样的,以便我根据条件过滤的所有行都与原始数据集中位于它们之前的行一起存储:

        x        y
1   correct 7.207544
2 incorrect 7.351168
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

r subset filter conditional-statements dplyr

1
推荐指数
1
解决办法
1522
查看次数

使用“&amp;&amp;”和“||” 在 C 中条件之外的语句中

我正在用 C 做一个 uni 作业,我得到了一个已经完成的程序来调试它,我已经走过这行代码

int main() {
FILE *personFile;

//...
//some code
//..

personFile && fclose(personFile);     //??
return 0;
}
Run Code Online (Sandbox Code Playgroud)

代替

if (personFile != NULL) 
    fclose(personFile);
Run Code Online (Sandbox Code Playgroud)

我认为fclose(personFile) 如果personFile为 NULL则永远不会发生,因为 C 快速评估“条件”并跳过 && 中的第二个参数,如果第一个参数为假,但我不确定

这真的是它的工作原理吗?这种“欺骗”条件的方式是不好的做法还是我很幸运从未见过这样的事情?

c boolean-logic operators conditional-statements

1
推荐指数
1
解决办法
692
查看次数

如何生成带有R中强加条件的随机数?

我想生成满足以下条件的 500 种不同的 a、b 和 c 组合

  1. a+ b+ c = 1 和
  2. a < b < c

这是生成随机数的基本示例,但是,我需要根据上述条件生成它。

Coeff = data.frame(a=runif(500, min = 0, max = 1),
b=runif(500, min = 0, max = 1),
c=runif(500, min = 0, max = 1))
Run Code Online (Sandbox Code Playgroud)

random if-statement r conditional-statements dataframe

1
推荐指数
1
解决办法
394
查看次数

如何根据特定条件交换 R 中的两列?

我有两列,比如 a & b,如果 b 列中的值大于 a 列中的值,我必须交换 a & b 的值。

如果满足条件,我已经编写了一个函数来交换变量

a     b
110   70
120   80 
80    110 

Run Code Online (Sandbox Code Playgroud)
swap_if<- function (a, b,missing=NA) 
{
  if(b>a)
  {
    a = a + b
    b = a - b
    a = a - b
  }
}  


swap_if(a,b)



Run Code Online (Sandbox Code Playgroud)

输出应为:

a     b
110   70
120   80 
110   80 

Run Code Online (Sandbox Code Playgroud)

但我收到一个错误

the condition has length > 1 and only the first element will be used
Run Code Online (Sandbox Code Playgroud)

swap if-statement r function conditional-statements

1
推荐指数
1
解决办法
630
查看次数

如何一次获得多个条件的结果

我什至不知道如何问我的问题!

我有一个这样的变量:

$value['state 1'] = true;
$value['state 2'] = false;
$value['state 3'] = false;
$value['state 4'] = true;
Run Code Online (Sandbox Code Playgroud)

我想保留一些关于每个状态的字符串,这些字符串false通过条件或其他方式具有true价值,如果所有状态都有价值,则有另一个字符串,例如:

$message = "";
if ($value['state 1'] !== true){
     $message .= "State one isn't working!\n";
} elseif ($value['state 2'] !== true){
     $message .= "State two isn't working!\n";
} elseif ($value['state 3'] !== true){
     $message .= "State three isn't working!\n";
} elseif ($value['state 4'] !== true){
     $message .= "State four isn't working!\n";
} else {
$message = "All states …
Run Code Online (Sandbox Code Playgroud)

php conditional-statements

1
推荐指数
1
解决办法
79
查看次数

Conditional types for nullable in Typescript

I want to check if a type is nullable or not, and if it has a conditional type on the value.

I tried implementing

type IsNullable<T> = T extends null ? true : false;
Run Code Online (Sandbox Code Playgroud)

However, it does not seem to work

type test = IsNullable<number> // Returns false as it should
type test = IsNullable<number | null> // Returns false when it should be true
Run Code Online (Sandbox Code Playgroud)

What's the proper way of checking if a type is nullable? I tried with T …

types nullable conditional-statements typescript

1
推荐指数
1
解决办法
414
查看次数