Multiple conditions in a ternary conditional operator?

I take my first semester of Java programming, and we just looked at the conditions of a conditional statement (? :). I have two questions that seem to want me to “embed” conditional statements in eachother, which I could easily (but tediously) do with if-else-if statements.

1) "Assume that the 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" mar ", apr" or "may" or "jun" or "jul" or "aug" or "sep" or "oct" or "nov" or "dec" depending on the value of the month. (So, if the month value was 4, then the value of the expression will be "apr".).

My idea looked 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 is not a complete expression, but I'm not sure how an operator phrase can handle so many conditions.)

2) Suppose that loans are an int variable whose value is 0 or positive. Write an expression whose meaning is “freshman” or “sophomore” or “junior” or “senior” based on the cost of loans. In particular: if the value of loans is less than 30, the value of the expression is “freshman”; 30-59 will be a sophomore, 60-89 will be a junior, and 90 or more will be a senior.

once again, I played, and the best I can come up with is something like (and I have spaces in some brackets):

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

I have Googled around and checked the database here, but I DO NOT THINK that there is something like this question; forgive me if I am wrong. The program (CodeLab) will not accept a Switch-Case or if-else-if solution, always suggesting to use a conditional expression ?: operator, but wherever I looked, I did not understand how to force the operator to handle so many conditions. We do not go far into this book, so if you guys could help me find a solution, it would be great if that were the case as soon as I found out about it.

+4
source share
6 answers

For the first question, you really can use a three-dimensional operator, but a simpler solution would be to use String[] with month descriptions, and then index 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 less conditions, although if it 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"; 
+11
source

Brackets resemble violence: if it does not work, use more.

But seriously:

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

And please, never write such code for anything important.

+4
source

(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

you understand this, the only thing you need is a zero at the end, when you finish everything.

+2
source

I had the same question in my office. Thanks for the info on if and else . It would be my choice, except that the task requires us to use special conditional statements. so basically they ask us to write it in an unreadable way.

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

It was mine and right. I am wondering if there is a shorter piece of code (using only conditional statements.).

By the way, Evan your code was almost good. just skipped some brackets around each expression.

+2
source

You are managing the idea of ​​if-else-if situations in triple order correctly, but your syntax has been slightly turned off (as you said, it could be).

I would, however, change it a bit so that extra conditions are not checked without necessity.

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

But your best option is to simply use the if and else statements for readability.

0
source

Put your code in brackets and add null to the end, and you are golden.

0
source

Source: https://habr.com/ru/post/1435706/


All Articles