三元条件运算符中的多个条件? [英] Multiple conditions in ternary conditional operator?

查看:655
本文介绍了三元条件运算符中的多个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java编程的第一学期,我们已经介绍了条件运算符(?:)条件.我有两个问题,似乎是想让我在彼此之间嵌套"条件运算符,而我可以很轻松地(但乏味地)使用if-else-if语句.

I am taking my first semester of Java programming, and we've just covered the conditional operator (? :) conditions. I have two questions which seem to be wanting me to "nest" conditional operators within eachother, something that I could easily (yet tediously) do with if-else-if statements.

1)假定月份是一个int变量,其值为1或2或3或5 ...或11或12.编写一个表达式,其值为"jan"或"feb"或"mar"或"apr"或"may"或"jun"或"jul"或"aug"或"sep"或"oct"或"nov"或"dec"基于月份的值.(因此,如果month的值为4,则表达式的值为"apr".).

1) "Assume that month is an int variable whose value is 1 or 2 or 3 or 5 ... or 11 or 12. Write an expression whose value is "jan" or "feb" or "mar" or "apr" or "may" or "jun" or "jul" or "aug" or "sep" or "oct" or "nov" or "dec" based on the value of month. (So, if the value of month were 4 then the value of the expression would be "apr".)."

我的主意看起来像这样:

an idea I had looks something like this:

(month==1)?"jan":(month==2)?"feb": (month==3)?"mar": (month==4)?"apr": 
(month==5)?"may":(month==6)?"jun": (month==7)?"jul":(month==8)?"aug":
(month==9)?"sep": (month==10)?"oct": (month==11)?"nov": (month==12)?"dec":

(我知道这不是一个完整的表达式,但是我不确定如何用词组运算符来处理这么多条件.)

(I know this isn't a complete expression, but I'm not sure how to phrase the operator to handle so many conditions.)

2)假设credits是一个int变量,其值为0或正数.根据学分的价值写一个值,其值为新人"或大二"或初中"或高级".特别是:如果学分的价值小于30,则表达式的价值为新鲜人"; 30-59将是大二",60-89将是大三",而90或更多将是大三".

2) Assume that credits is an int variable whose value is 0 or positive. Write an expression whose value is "freshman" or "sophomore" or "junior" or "senior" based on the value of credits. In particular: if the value of credits is less than 30 the expression's value is "freshman"; 30-59 would be a "sophomore", 60-89 would be "junior" and 90 or more would be a "senior".

再次,我一直在玩耍,我能想到的最好的东西是这样的(而且我很可能遗漏了一些必要的括号):

again, I've been toying around and the best I can come up with is something like(and I'm probs missing some necessary parentheses):

credits < 30 ? "freshman": credits >= 30 && <=59 ?
 "sophomore": credits >= 60 && <= 89 ? "junior": "senior"

我已经在Google周围搜索并检查了这里的数据库,但是我不认为有什么与这个问题完全相同.如果我错了,请原谅我.程序(CodeLab)不会采用Switch-Case或if-else-if解决方案,总是提示我应该使用条件? :运算符,但是我一直到哪里都找不到解决如何操纵该运算符的方法.我们在本书中还不算太远,所以如果你们能帮助我找到解决方案,那么这是一件很棒的事,如果我能从我到目前为止所学的知识中汲取一点灵感的话,那就太好了.

I've Googled around and checked the database here, but I don't THINK that there's anything exactly like this question; forgive me if I'm wrong. The program (CodeLab) won't take Switch-Case or the if-else-if solution, always suggesting I should be using the conditional ? : operator, but everywhere I've looked I haven't figured out how to rig the operator to handle so many conditions. We aren't far past this in the book, so if you guys could help me find a solution, it'd be great if it's one that jives with the little bit I've learned so far.

推荐答案

对于 first 问题,您可以确实使用三元运算符,但是更简单的解决方案是在月份说明中使用String[],然后对该数组下标:

For the first question, you can indeed use the ternary operator, but a simpler solution would be to use a String[] with the month descriptions, and then subscript this array:

String[] months = { "jan", "feb", "mar", ... };
int month = 1; // jan
String monthDescription = months[month - 1]; // arrays are 0-indexed

现在,对于您的 second 问题,三元运算符似乎更合适,因为您的条件较少,尽管if会更容易阅读,恕我直言:

Now, for your second question, the ternary operator seems more appropriate since you have fewer conditions, although an if would be much easier to read, imho:

String year = "senior";
if (credits < 30) {
  year = "freshman";
} else if (credits <= 59) {
  year = "sophomore";
} else if (credits <= 89) {
  year = "junior";
}

将此与三元运算符进行对比:

Contrast this with the ternary operator:

String year = credits < 30 ? "freshman" : credits <= 59 ? "sophomore" : credits <= 89 ? "junior" : "senior";

这篇关于三元条件运算符中的多个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆