标签: conditional-operator

PHP中的条件运算符快捷方式

有人知道PHP中是否有以下语句的快捷方式?

$output = isset($some_value) ? $some_value : "Some Value Not Set";
echo $output;
Run Code Online (Sandbox Code Playgroud)

这是我经常遇到的事情,其中​​$ some_value实际上很长并且可能涉及一个函数,例如:

$output = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value) ? $this->db->get_where('my_db',array('id'=>$id))->row()->some_value) : "Some Value Not Set";
echo $output;
Run Code Online (Sandbox Code Playgroud)

似乎应该有一个操作员或功能来执行此操作.我可以很容易地写一个,我不是在寻找答案,而是如果有人知道内置的快捷方式.

php conditional-operator

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

当变量等于零时隐藏标签

我想隐藏一个StopWatch标签,当它等于0时,我怎么能用条件运算符呢?

using System;
using System.Diagnostics;
using System.Threading;

namespace FileTransfer_Socket_Client
{
class transferRate
{
    static Stopwatch stopWatch = new Stopwatch();
    public static void timeLeft()
    {
        Thread StimeLeft = new Thread(Start);
        StimeLeft.Start();
    }

    private static void Start()
    {
        int rate = 0;
        int left = 0;
        int prevSum = 0;
        stopWatch.Start();
        while (fileTransfer.client.Connected)
        {
            if (fileTransfer.sum != 0)
            {
                rate = (fileTransfer.sum-prevSum)/1024;
                left = ((fileTransfer.fileSize - fileTransfer.sum)/ 1024) / rate;
                TimeSpan t = TimeSpan.FromSeconds(left);
                Program.mainForm.AppendLabel(string.Format("{0}kb/s  timeleft: {1:D2}:{2:D2}:{3:D2}", rate, t.Hours, t.Minutes, t.Seconds));
                prevSum …
Run Code Online (Sandbox Code Playgroud)

c# conditional-operator

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

goto不能使用?:运算符

出于学习目的,我编写了以下代码片段:

for(int i=0;i<10;i++)
{
  for(int j = 0;j<5;j++)        
  {
    //(i==j && i==3)? (goto found) : printf("stya here\n");        
    if(i==j && i==3){goto found;} else {printf("stay here\n");}
  }
}

found:
  printf("yes I am here");
Run Code Online (Sandbox Code Playgroud)

但我想知道我什么时候发现内部循环中的省略语句没有给出错误,现在我很困惑if-else并不总是可以用?:运算符替换.这是什么事实?为什么评论语句会出错?

c goto conditional-operator

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

如何在属性属性上使用条件语句IF ELSE

我想显示MyProperty1MyProperty2基于MyPropertySelected.如何使用条件语句ifelse基于MyPropertySelected?谢谢.

// [Browsable(true)
// ????? conditional statement IF ELSE in here..
// IF (MyPropertySelected) MyProperty1 will be show ELSE MyProperty2 will be show.
public bool MyPropertySelected { get; set; }

// [Browsable(true) or [Browsable(false) depending on MyPropertySelected condition.
public int MyProperty1 { get; set; }

// [Browsable(true) or [Browsable(false) depending on MyPropertySelected condition.
public int MyProperty2 { get; set; }
Run Code Online (Sandbox Code Playgroud)

c# attributes properties conditional-operator winforms

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

'void'和'<null>'之间的隐式转换?

我想在这里做一件非常简单的事 -

我需要在表格布局面板中找到一个控件

1)如果控件存在 - 删除控件

2)否则我不需要做任何事情

除了if\else之外,我决定使用条件运算符.我的代码是:

var temp=(tableLayoutExamPanel.Controls.Find("lbl3", true)[0].Name==("lbl3")) ? (tableLayoutExamPanel.Controls.Find("lbl3", true)[0].Dispose()) : null ;
Run Code Online (Sandbox Code Playgroud)

但我面临这个错误:

Type of conditional expression cannot be determined because there is no implicit conversion between 'void' and '<null>' 
Run Code Online (Sandbox Code Playgroud)

谷歌有这么多的解决方案,但没有一个适合我,我没有找到任何无效的解决方案,陷入其中,需要帮助.

提前致谢.

c# conditional-operator winforms

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

在某种情况下,你能否"不等于"一个范围?

有没有办法让条件为if语句(x "not"<= 90)?或者是唯一的方法,(x <= 90)if零件中使用,如果是真的则无所事事,然后执行需要做的任何事情else?我应该提到我在Java中这样做.

java if-statement conditional-operator conditional-statements

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

条件表达式返回类型

我有两个类实现IMyInterface返回类型的异步方法Task<IMyInterface>.

为什么我的return语句收到编译器错误"没有隐式转换类型" return SomeBooleanDeterminedByTheMethod ? Class1 : new Class2();,解决此问题的最佳步骤是什么?

完整方法:

public static async Task<IMyInterface> MyMethodAsync(Subclass1 Class1Child)
{
    var listOfThings = new List<Tuple<int, Class1>>();
    await Task.Run(() =>
    {
        foreach (var item in SomeCollection)
        {
            var DummyClass1 = new Class1() {IntProperty = 0};
            var computationResult = new Tuple<int, Class1>( DummyClass1.IntProperty, DummyClass1);
            listOfThings.Add(computationResult);
        }
    }

    try 
    {
        var returnedClass1 = (from items in listOfThings
                          orderby items.Item1
                          select items.Item2).FirstOrDefault();
        return returnedClass1.BooleanProperty ? returnedClass1 : new Class2();
    }
    catch ... …
Run Code Online (Sandbox Code Playgroud)

c# conditional-operator

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

使用条件运算符时变量值如何变化?

任何人都可以帮我解释上面代码的输出..它将在不同的编译器中打印不同的输出.哪一个要考虑.

#include<stdio.h>

int main()
{
    int a=0, b=1, c=2;
    *((a+1 == 1) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d\n", a, b, c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

产量

0,2,2
Run Code Online (Sandbox Code Playgroud)

此输出来自Codeblocks

c pointers ternary-operator conditional-operator

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

使用条件运算符的简明方法

我有这个代码,这LongMethodWithResult是一个需要很长时间才能运行的方法:

object o = LongMethodWithResult() == someVal ? LongMethodWithResult() : someOtherResult;
Run Code Online (Sandbox Code Playgroud)

现在这个LongMethodWithResult方法被评估了两次,不是吗?

我知道我可以编写一个方法,使用变量来存储long方法的结果,如下所示:

public static object ConciseConditionalOperator(object a, object b, object c)
{
    return a == b ? a : c;
}
Run Code Online (Sandbox Code Playgroud)

但我会对是否有最好的方法,或者C#或.NET提供的某些功能感兴趣.

欢迎任何想法!

c# conditional-operator

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

Python三元运算符添加整数

我试图在python中进行三元运算,如果money == 100,则在数组中的项目中添加1,如果不是,则在另一个项目中添加1.但我继续得到无效的语法错误.

 bills[2] += 1 if money == 100 else bills[1] += 1
                                             ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

这是代码.

def tickets(people):
change =0 
bills = [0,0,0]
for i,money in enumerate(people):
    if money == 25:
        change += 25
        bills[0] += 1
        str = "This is the %d th person with %d money" % (i,money)
        print(str)

    else:
        bills[2] += 1 if money == 100 else bills[1] += 1
        change -= (money -25)
        str = "This is the %d th person with %d …
Run Code Online (Sandbox Code Playgroud)

python conditional-operator python-3.x

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