我不理解以下行中使用的语法,除了它遵循似乎被称为三元运算符的基本结构.
string path = args == null || args.Length == 0 ?
@"C:\GENERIC\SYSTEM\PATH" :
args[1];
Run Code Online (Sandbox Code Playgroud)
我是这种语法的新手.有人会帮助我把它翻译成真正的英语(或伪代码),就像if语句可以变成"如果那样那样"的方式一样吗?
编辑:谢谢大家的答案,你们都非常乐于助人.不幸的是我只能投票给你们其中一人,但我会投票给你们!
我正在尝试使用三元运算符切换按钮文本,但它不起作用。
<button type="button" id="ter" >Bar</button>
$("#ter").click(function(){
$(this).text() = 'Bar' ? 'column':'Bar';
});
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?我做错了什么?我知道有很多其他方法可以实现这一点,但我只需要知道这种方法是否可行?
我无法破译这行代码.有人可以把它翻译成if/ else陈述吗?
我理解基本CONDITION ? VALUE_IF_TRUE : VALUE_IF_FALSE模式,但这条线似乎打破了这一点.
$type = $self->{1}{_flag} & 2 ?
$self->{2}{_flag} & 2 ? "A" : "B" :
$self->{2}{_flag} & 2 ? "B" : "C";
Run Code Online (Sandbox Code Playgroud) 例如,有没有办法仅基于一个条件在 C 中打印多个字符串
int main(int argc, char const *argv[]) {
printf("%s %s\n", condition ? "string1","string2" : "string3","string4");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果条件为真,那么我想要
string1 string2
Run Code Online (Sandbox Code Playgroud)
作为输出但是当我这样做时,它不是我得到的
我试图告诉我的 WordPress 模板使用特定的开放图形图像,如果它当前在主页上。WordPress 有一个内置功能来确定您当前是否在主页上:
is_front_page().
Run Code Online (Sandbox Code Playgroud)
我的模板在 header.php 中有这个:
<meta property="og:image" content="<?php echo the_post_thumbnail_url('mint-full-post'); ?>" />
Run Code Online (Sandbox Code Playgroud)
我把它改成这样:
<meta property="og:image" content="<?php is_front_page() ? echo 'http://rainydaystories.com/wp-content/uploads/social.jpg' : echo the_post_thumbnail_url('mint-full-post'); ?>" />
Run Code Online (Sandbox Code Playgroud)
这会导致网站崩溃。我做错了什么,谁能帮我找到更好的解决方案?谢谢!
我正在使用三元运算符有条件地呈现页脚。我正在做@RenderPage,即使它有效,也意味着有一个控制器以及其他一些额外的代码。
我遇到了一个; expected错误,根据 StackOverflow 和一些文档,这是一个通用错误,可能是由许多不同的事情引起的。
我的语法正确吗?或者有什么我想念的吗?
// in this section is a switch statement that sets isNewFooter to true or false depending on which page has loaded.
<div class="body-content">
@RenderBody()
@{
(isNewFooter ? Html.RenderPartial("~/Views/Shared/NewFooter.cshtml") : Html.RenderPartial("~/Views/Shared/OldFooter.cshtml"))
}
</div>
Run Code Online (Sandbox Code Playgroud) 我尝试了很多次来了解如何工作或什么是语法代码呢?
greatest=(a>b&&a>c)?a:(b>c)?b : c;
Run Code Online (Sandbox Code Playgroud)
我知道三元运算符的主要语法
但最重要的是我不知道它是怎么来的
谢谢。
我正在映射一个名为 items2
items2: [
{
id: 1,
title: 'TRENDING ON THE COURT',
image1: 'https://c.static-nike.com/a/images/f_auto/dpr_2.0/h_500,c_limit/8ef2ada7-0cf4-43d4-a0a1-4a60e3071b06/jordan.jpg',
image2: 'https://c.static-nike.com/a/images/f_auto/dpr_2.0/h_500,c_limit/1b387797-073a-4f60-8b67-9485c23ca556/jordan.jpg',
image1title: 'AIR JORDAN XXXIV SE',
image2header: 'L.A BORN',
image2title: 'JORDAN "WHY NOT?" ZERO.3',
a1text: 'Shop',
a2text: 'Shop'
},
{
id: 2,
title: 'TRENDING ON THE STREETS',
image1: 'https://c.static-nike.com/a/images/f_auto/dpr_2.0/h_500,c_limit/ac454c04-2d8b-4cf5-b73a-7245545b6f7c/jordan.jpg',
image2: 'https://c.static-nike.com/a/images/f_auto/dpr_2.0/h_500,c_limit/de7332be-8c0d-405f-af9e-201ba9f61ab3/jordan.jpg',
image1title: 'JORDAN AIR MAX 200',
image2title: "JORDAN WOMEN'S FLIGHT CAPSULE",
a1text: 'Shop',
a2text: 'Shop'
},
{
id: 3,
title: 'MEMBER ACCESS',
image1: 'https://c.static-nike.com/a/images/f_auto/dpr_2.0/h_500,c_limit/2225370e-a461-4ea3-ac94-3ece70dc3575/jordan.jpg',
image2: 'https://c.static-nike.com/a/images/f_auto/dpr_2.0/h_500,c_limit/037f5213-a631-4e91-9c70-9187aefda947/jordan.jpg',
image1header: "'JORDAN UNITE COLLECTION'",
image1title: 'AIR JORDAN III',
image2title: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,如果该数字是整数,则该程序接受一个数字并删除尾随的零。我正在使用三元运算符,但它没有按预期工作。但是如果我把它写成 if else 语句,它就可以工作。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double number = scanner.nextDouble();
System.out.println(((int)number == (double)number) ? (int)number : number); // Always outputs a double
if ((int)number == (double)number) { // Outputs correct result
System.out.println((int)number);
}
else {
System.out.println(number);
}
}
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我输入 5 我得到
5.0
5
Run Code Online (Sandbox Code Playgroud)
如果我输入 7.3 我得到
7.3
7.3
Run Code Online (Sandbox Code Playgroud)
所以它似乎适用于 if else 语句但不适用于三元运算符。
我是 javascript 新手,我似乎无法解决我遇到的一个小问题。我四处寻找,尝试了许多其他选择,但似乎没有任何效果。此功能工作正常,但我收到此错误消息:
error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
const valid = (email) => {
// TODO: return true if the `email` string has the right pattern!
const match = (email.match(/^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})$/) ? true : false);
return match;
};
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能用不同的方式写这个?预先感谢您的帮助!奥利维尔