我已经看到可以使用多个三元条件,但是如果单个条件为真,则没有找到分配两个变量的方法.这是我试图写的方法:
int[] chkNext(int mnd, int y) {
int[] date = new int[2];
mnd = 12 ? mnd = 1, y++ : mnd++; // returns the following: "error: : expected"
date[0] = mnd, date[1] = y;
return date;
}
Run Code Online (Sandbox Code Playgroud) 有没有办法让三元运算符做到这一点?:
if (SomeBool)
SomeStringProperty = SomeValue;
Run Code Online (Sandbox Code Playgroud)
我能做到这一点:
SomeStringProperty = someBool ? SomeValue : SomeStringProperty;
Run Code Online (Sandbox Code Playgroud)
但即使SomeBool为假(右),这也会触发SomeStringProperty的getter和settor?所以它与上述陈述不同.
我知道解决方案是不使用三元运算符,但我只是想知道是否有办法忽略表达式的最后部分.
不是三元运算符假设像arg一样工作?真假 ???所以如果持续时间和石油超过规定的数量而不是字段变量,它应该返回true ..但是这会返回false
public class test12 {
int duration = 260;
int petroleum = 300;
boolean result;
public void checktrain(){
boolean result = duration>=250 && petroleum>=235? true : false;
this.result = result;
}
public void run(){
System.out.print(result);
}
public static void main(String args[]){
test12 tr = new test12();
tr.run();
}
}
Run Code Online (Sandbox Code Playgroud) Errors are illegal start of an expressionerror: not a statement';' expected我在takeStix()中收到有关if else语句的错误.
private int numStix;
public int getNumStix() {return numStix;}
public boolean takeStix(int number) {
( number <= 3 && number <= getNumStix() ) ? return true : return false;
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码的简化版本:
var cond = true;
var res1 = "400" + cond ? "%" : "px";
var res2 = "400" + (cond ? "%" : "px");
Run Code Online (Sandbox Code Playgroud)
结果如下:
res1 = "%";
res2 = "400%";
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?似乎两个语句都应该等于"400%" - 为什么第一个语句不能取出字符串的第一部分?
我对 React 比较陌生,正在开发 John Conway - Game of Life 应用程序。我Gameboard.js为棋盘本身(它是 的子项App.js)构建了一个功能组件,以及一个Square.js代表棋盘中单个方块的功能组件(并且是 的子项Gameboard和孙子App)。
在App我有一个调用的函数alive,当用户单击它时,我想更改单个方块的颜色。App也有一个 'alive' 属性,它的状态最初设置为 false,并alive在调用时将该属性更改为 true。
这是App.js:
import React, { Component } from 'react';
import './App.css';
import GameBoard from './GameBoard.js';
import Controls from './Controls.js';
class App extends Component {
constructor(props){
super(props);
this.state = {
boardHeight: 50,
boardWidth: 30,
iterations: 10,
reset: false,
alive: false
};
}
selectBoardSize = (width, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 dict 理解和 python 中的三元运算来执行以下表达式:
for num in ar:
if num in seen_dict:
seen_dict[num] += 1
else:
seen_dict[num] = 1
Run Code Online (Sandbox Code Playgroud)
我试过这个:
seen_dict = { num: seen_dict[num] += 1 for num in ar if num in seen_dict else seen_dict[num] = 1}
Run Code Online (Sandbox Code Playgroud)
及其几个排列,但我不断收到语法错误。可以做我想做的吗?
更新
这是正确的语法,但不是我的 dict 只返回 1:
seen_dict = { num: (seen_dict[num] + 1) if num in seen_dict else 1 for num in ar }
有人可以解释为什么这与 for 循环的功能不同吗?谢谢你。
这是我的三元:
mode === 'edition' ? (this.editionMode = true, this.creationMode = false) : (this.creationMode = true, this.editionMode = false)
Run Code Online (Sandbox Code Playgroud)
我认为这是多余的,我可以用更好的方式编写这个三元吗?谢谢 !
以下程序的输出是: 3 1 3
int main()
{
int a = 0, b = 1, c = 3;
*((a) ? &b : &a) = a ? b : c; // Couldn't understand expression
printf("%d %d %d \n", a, b, c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
三元运算符如何在此处输出此输出?
所以,我只想知道是否可以在for循环的终止位内的任何代码或三元运算符中滑动.如果可能的话,你能在for循环中提供一个三元运算符的例子吗?谢谢!
for (initialization; termination; increment) {
statement(s)
}
Run Code Online (Sandbox Code Playgroud) ternary ×10
java ×4
javascript ×2
.net ×1
boolean ×1
c ×1
c# ×1
classname ×1
css ×1
python ×1
python-3.x ×1
react-props ×1
reactjs ×1
return ×1
termination ×1
vue.js ×1