20

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) "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) 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"

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.

4

7 回答 7

40

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

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";
于 2012-09-23T00:54:22.247 回答
10

Parentheses are like violence: if it's not working, use more.

But seriously:

( condition A ? value A :
  ( condition B ? value B : 
    ( condition C ? value C :
       ...
    )
  )
)

And please, don't ever write code like that for anything important.

于 2012-09-23T00:50:37.523 回答
4

(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":null

你做对了,你唯一需要的就是当你完成这一切时最后的空值。

于 2012-10-22T13:36:16.460 回答
3

我在学习时也有同样的问题。感谢您提供有关if和的信息else。除了作业要求我们专门使用条件运算符外,这也是我的选择。所以基本上他们要求我们以不可读的方式编写它。

(credits < 30) ? "freshman" : (credits >= 30 && credits < 60) ?"sophomore" : (credits >= 60 && credits < 90) ? "junior" : "senior"

这是我的,它是正确的。我想知道是否有更短的代码(仅使用条件运算符。)。

顺便说一句,埃文你的代码几乎是好的。只是错过了每个表达式周围的一些括号。

于 2012-11-06T22:14:28.823 回答
0

You are handling the idea of a if-else-if situation in a ternary correctly, but your syntax was slightly off (as you said it might be).

I would, however, change it slightly so that extra conditions aren't checked unnecessarily.

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

But your best option is just to use if and else statements for the sake of code readability.

于 2012-09-23T00:51:58.873 回答
0

-在我的情况下,我有一个“isloadingMakePayment”标志,基于我必须在该标签上实现加载栏的标志。这意味着当isloadingMakePayment为真时,标签显示“正在加载数据......”。实现了多个条件。

- “isPolicyRename”也是我用于内部条件的标志。MeanWhile“renamePolicy”是变量,其中包含一些我用来显示在 HTML 页面上的值

  • 只需使用此模式:

<a class="btn btn-orange" (click)="onMakePayment()"> <span *ngIf="isloadingMakePayment"><i class='fa fa-spinner fa-spin'></i>Loading Data...</span>{{isloadingMakePayment ? '' : isPolicyRename 'Make Payment to ' + renamePolicy : 'Make Payment' }} </a>


谢谢你。

于 2020-03-14T12:25:13.433 回答
0

将您的代码放在括号中并在末尾添加 null ,您就可以了。

于 2016-09-24T02:56:16.190 回答