If statement 我想写三元运算符,等价于下面的if条件
我把它写成If statement 我想写三元运算符,等价于下面的if条件,if-statement,If Statement,我把它写成 String destinationFile = request.getParameter("destinationFile "); if (destinationFile == null || "".equals(destinationFile)) response.sendRedirect("/astro/login/index.jsp?destinationFile =customerLogin.jsp"); terinary运算符的问题是,应在以下位置放置什么: 在这种情
String destinationFile = request.getParameter("destinationFile ");
if (destinationFile == null || "".equals(destinationFile))
response.sendRedirect("/astro/login/index.jsp?destinationFile =customerLogin.jsp");
terinary运算符的问题是,应在以下位置放置什么:
在这种情况下,我没有提到任何其他。我必须验证目录结构是否应该被挂起到destinatedFile。您不能这样做 三元运算符生成一个表达式,可用于e。G作为函数参数 也就是说,如果您有一个
else
分支也会发送一些东西,那么可以使用terary操作符
所以
可以改写为
if (a) {
response.sendRedirect(b);
} else {
response.sendRedirect(c);
}
但是,如果您的else
分支执行完全其他的操作(或者完全不执行任何操作,就像您的情况一样),您将被困在正常的if
子句中。使用三元运算符-->
你不能那样做。。。
三元运算符涉及IF-ELSE条件,并且您只有IF部分
例如,假设您有以下代码:
var reult =
(request.getParameter("destinationFile").ToString() != String.Empty || request.getParameter("destinationFile") != null) ? response.sendRedirect("/astro/login/index.jsp?destinationFile =customerLogin.jsp") :
null;
然后您可以将其更改为:
String destinationFile = request.getParameter("destinationFile ");
if (!String.IsNullOrEmpty(destinationFile))
response.sendRedirect(destinationFile);
else
response.sendRedirect("/astro/login/index.jsp?destinationFile=customerLogin.jsp");
你为什么要这么做?
var reult =
(request.getParameter("destinationFile").ToString() != String.Empty || request.getParameter("destinationFile") != null) ? response.sendRedirect("/astro/login/index.jsp?destinationFile =customerLogin.jsp") :
null;
String destinationFile = request.getParameter("destinationFile ");
if (!String.IsNullOrEmpty(destinationFile))
response.sendRedirect(destinationFile);
else
response.sendRedirect("/astro/login/index.jsp?destinationFile=customerLogin.jsp");
String destinationFile = request.getParameter("destinationFile ");
response.sendRedirect(!String.IsNullOrEmpty(destinationFile) ? destinationFile : "/astro/login/index.jsp?destinationFile=customerLogin.jsp"));