标签: conditional-operator

PHP中的嵌套条件语句

如何在PHP中创建嵌套的条件语句?

我试着:

if (A=="blah" && B=="bleh" && C=="bloh" && (D=="bluh" || E=="blih"))
Run Code Online (Sandbox Code Playgroud)

更易于阅读:

if (A AND B AND C AND (D OR E))
Run Code Online (Sandbox Code Playgroud)

php conditional if-statement conditional-operator

-2
推荐指数
1
解决办法
952
查看次数

如果数字非零则如何打印,否则使用printf打印?

我有以下功能,如果我的值是0,它会给出一些奇怪的输出.有人可以解释一下为什么这表现得很奇怪吗?我的意图是仅在非零值时打印该值,否则应为空白

# include<stdio.h>

int main(int argc, char *argv[]) {
    int i=0;
    printf("Number is %d\n", i ? i : "");
    return 0;
}

-> gcc print.c
print.c: In function 'main':
print.c:5: warning: pointer/integer type mismatch in conditional expression
-> ./a.out
Number is 4195848
Run Code Online (Sandbox Code Playgroud)

我知道我可以像下面这样做,但我想用上面的逻辑做同样的事情

if(i != 0) {
    printf("%d", i);
} else {
    printf("%s", "");

}
Run Code Online (Sandbox Code Playgroud)

c printf ternary-operator conditional-operator

-2
推荐指数
1
解决办法
1349
查看次数

Java:条件运算符的奇怪行为

我有一个非常简单的 java 类来使用递归方法解决解码方法。我看到条件运算符的这种奇怪行为,

package decodeways;

public class Solution {
  public static void main(String[] args) {
    System.out.println(numDecodings("1456"));
  }

  public static int numDecodings(String s) {
    if(s.length()>0 && s.charAt(0)=='0') 
      return 0;
    if(s.length()==0) return 1;
    if(s.length()==1)
      return 1;

    int num1 = s.charAt(0)-'0';
    int num2 = s.charAt(1)-'0';
    int one = numDecodings(s.substring(1));
    int two = s.length()>1?numDecodings(s.substring(2)):0;
    int res = one 
        + num1<3 && num2<7 ? two:0;
    return res;
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我加上括号,(num1<3 && num2<7 ? two:0)那么一切都很好,但是如果我去掉括号,就会得到不正确的结果。

在调试过程中,一个计算为1,两个计算为1,res也为1,带括号,但没有括号,res的计算结果将为0(附截图),这是错误的来源. 在此处输入图片说明 我知道 java …

java conditional-operator

-2
推荐指数
1
解决办法
82
查看次数

嵌套的三元运算符未按预期返回

我正在尝试围绕嵌套的三元操作进行思考。

colorVariant 的值是“成功”、“信息”、“错误”或“默认”。

props.colorVariant == 'success' ? 'green' : 'info' ? 'gray' : 'error' ? 'red' : 'default' ? 'black' : 'purple'
Run Code Online (Sandbox Code Playgroud)

目前,我已将该值设置为“默认”,因为我希望看到它一直到最后,但就目前而言,返回的颜色是灰色的。

有人可以解释为什么会这样吗?

javascript conditional-operator

-2
推荐指数
1
解决办法
41
查看次数

为什么比较运算符无法正常工作而运算符“ne”块正在工作

我有一些从命令行获取的字符串作为参数,如果特定参数已被传递,我需要运行该块,否则它会显示打印语句并退出。

我正在使用两个块,其中在第一个块中我使用ne运算符来检查字符串是否不等于所需的字符串,然后退出程序并eq运算符来检查传递的字符串是否相等,然后执行代码。

下面是我的代码片段:


 my $number_args = $ #ARGV + 1;
 if ($number_args ne 2) {
         print "Usage: perl testAutomation.pl <ExcelSheetName.xlsx> <Production/Development/All>.\n";
         exit;
 }
 my $tstRunSheet = $ARGV[0];
 my $tstRunType = $ARGV[1];

 if ($tstRunType ne 'Development' || $tstRunType ne 'Production' || $tstRunType ne 'All') {
         print "Usage: perl testAutomation.pl <ExcelSheetName.xlsx> <Production/Development/All>.\n";
         exit;
 }
 elsif($tstRunType eq 'Development' || $tstRunType eq 'Production') {
         ($result1, $result2, $result3, $result4, $result5, $result6, $result7, $result8) = & getXlxsDetails($tstRunSheet, $tstRunType);
 }

Run Code Online (Sandbox Code Playgroud)

当我根据elsif块中的要求提供正确的字符串时,它仍然在运行该 …

perl conditional-operator

-2
推荐指数
1
解决办法
123
查看次数

C语言操作员

#include <stdio.h>  

int main() 
{

    int a=-1?2:5 + 8?4:5;
    printf("%d\n",a);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

以上程序的输出是2.但为什么呢?请解释

c operators ternary-operator conditional-operator operator-precedence

-3
推荐指数
1
解决办法
68
查看次数

如何链接三元运算符?

我根据多种条件下三元运算符中的值显示不同的数据:

我有这个,

 [attr.data-pendo]="linkedItemType === LinkedItemType.Prospect ? 'pendo-prospects' : 'pendo-loans'"
Run Code Online (Sandbox Code Playgroud)

我还需要添加 1 个条件,

所以它就像:

 if linkedItemType === LinkedItemType.Prospect then 'pendo-prospects' 
 if linkedItemType === LinkedItemType.Loan then 'pendo-loan' 
 else 'pendo-task'
Run Code Online (Sandbox Code Playgroud)

如何在三元中实现这个?

c# conditional-operator

-3
推荐指数
1
解决办法
1万
查看次数

按字符串类型的多个属性对对象数组进行排序

我在 JavaScript 中根据名称对数组进行排序,然后我根据 injoinDate对其进行排序,但不知何故它没有检查joiningDate.

不幸的是,我不能使用 if-else 条件 - 我只想使用三元运算符。

我的代码是这样的:

person.sort(((a, b) => (a.name > b.name)  ? 1 : (a.joinDate > b.joinDate) ? 1 : -1));
Run Code Online (Sandbox Code Playgroud)

它正在对名称进行排序,但不对joinDate属性进行排序

我的清单是这样的:

{
"data": [
     {
         "id": "fdsf",
         "name": "Julie",
         "joinDate": "01/10/2019"

      },
    ]
}
Run Code Online (Sandbox Code Playgroud)

javascript arrays conditional-operator reactjs react-native

-3
推荐指数
2
解决办法
875
查看次数

三元运算符与数组?

在 C 中,索引数组是否比?:运算符更快?

例如,会(const int[]){8, 14}[N > 10]N > 10? 14 : 8?

c arrays performance conditional-operator micro-optimization

-3
推荐指数
1
解决办法
119
查看次数

当用“or”和“?”比较两个值时,C++ 中的奇怪行为 操作员

我在一个旧的桌面应用程序中有一些代码,这是我从一位离开公司的同事那里继承的。这是我实际代码的一个简短的类似示例:

#include <iostream>
int getType(int type)
{
    return type;
}

int main()
{
    int variable1 = 30;
    int variable2 = 40;
    int result = (getType(30) == (variable1 || variable2) ? 1 : 2);
    std::cout << result << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

对于结果,我总是收到 2,而不是我预期的 1。如果我尝试:

int result = (getType(30) == (variable1) ? 1 : 2)

结果是真的。

我不明白为什么这部分:

int result = (getType(30) == (variable1 || variable2) ? 1 : 2)

不是真的……

c++ initialization conditional-operator comparison-operators logical-or

-3
推荐指数
1
解决办法
104
查看次数