本质上我有以下问题:
if condition A & B are true ->. do thing A
if only condition A is true -> do thing B
else -> do thing C
Run Code Online (Sandbox Code Playgroud)
我试过这个:
const myThing = conditionA ? conditionB ? thingA :
conditionA ? thingB : thingC;
Run Code Online (Sandbox Code Playgroud)
它不喜欢这种语法,但我不确定它有什么问题。
我正在做 LeetCode 437 Path Sum III https://leetcode.com/problems/path-sum-iii/ ,我的原始代码如下,通过了所有测试:
public int pathSum(TreeNode root, int sum) {
if (root == null) {
return 0;
}
return pathSumStartWithRoot(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
}
private int pathSumStartWithRoot(TreeNode root, int sum) {
if (root == null) return 0;
int res = root.val == sum ? 1 : 0;
return res
+ pathSumStartWithRoot(root.left, sum - root.val)
+ pathSumStartWithRoot(root.right, sum - root.val);
}
Run Code Online (Sandbox Code Playgroud)
我的困惑来自int res = root.val == sum ? 1 : 0; …
我有一个在某些情况下返回 null 的函数,例如:
public Class Foo {
public static Double bar(double a, double b) {
if (a == 0 || b == 0) return null;
return a + b;
}
}
Run Code Online (Sandbox Code Playgroud)
而且我想创建另一个函数,它执行完全相同的操作,只是在满足该条件时抛出错误而不是返回 null。我尝试这样做:
public Class Foo {
public static Double bar(double a, double b) {
if (a == 0 || b == 0) return null;
return a + b;
}
public static Double barWithException(double a, double b) {
return bar(a, b) == null ? throw new IllegalArgumentException() : bar(a, b); …Run Code Online (Sandbox Code Playgroud) 我目前正在使用这种技术:
...
string s = user.Gender ? "Mr." : "Mrs.";
string body = $@"Dear {s} {user.First_name}..."
...
Run Code Online (Sandbox Code Playgroud)
我想做类似的事情:
...
string body = $"Dear {return user.Gender ? "Mr." : "Mrs."} {user.First_name}..."
...
Run Code Online (Sandbox Code Playgroud) 是否可以根据状态变化设置三元组内变量的值?
就像是:
{ this.state.change ? myObj.changeIt = true : null }
Run Code Online (Sandbox Code Playgroud)
像这样写会返回错误: Assignment to property of function parameter 'myObj'
javascript conditional-operator setstate ecmascript-6 reactjs
我对 Java 还很陌生,我试图检查一个变量是否为空,如果不是,则使用它的值。以前的开发人员写的东西是这样的:
xModel.setName(xService.getName(xID) != null ? xService.getName(xID) : "");
Run Code Online (Sandbox Code Playgroud)
我想重构它,这样我就不必两次使用 xService 来获得名称。
我知道我可以预先存储该值,但这只是一个示例。我只是想知道是否有办法在 Java 中做到这一点?
谢谢。
为什么以下代码的输出为“0”?
#include <bits/stdc++.h>
int main() {
int max = 5;
std::cout << (false) ? "impossible" : std::to_string(max);
}
Run Code Online (Sandbox Code Playgroud) 有可能做这样的事情吗?
dog.setIsBarkingIndicator(mailman.getIsAtDoor() != null && mailman.getIsAtDoor().equals("N") ? false : true);
但是对于我研究的内容,这基本上与以下内容相同: dog.setIsBarkingIndicator(mailman.getIsAtDoor() == null || !mailman.getIsAtDoor().equals("N"))
因此,如果它的 null 或不等于“N”,它实际上将其设置为 false?我理解正确吗?
有没有办法在不使用 if 条件的情况下检查空值?
谢谢!
在这里反应原生初学者。我有一个按钮,其中的文本将根据条件进行更改。
所以如果"this.props.X ==='a'"按钮文本将是'start',
如果this.props.X ==='b'按钮文本将是'stop'。我怎样才能做到这一点?我可以在按钮标签内使用条件运算符吗?
我正在尝试这样但不起作用。
<Button
type = "primary"
(this.props.X === 'a'? title = {'Start'} : title = {'Stop'})
>
</Button>
Run Code Online (Sandbox Code Playgroud) java ×4
javascript ×3
c++ ×2
reactjs ×2
c ×1
c# ×1
c#-6.0 ×1
c++11 ×1
ecmascript-6 ×1
react-native ×1
return ×1
setstate ×1