Multiple conditions in ternary conditional operator?

后端 未结 7 863
醉酒成梦
醉酒成梦 2021-01-04 12: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 \"n

7条回答
  •  走了就别回头了
    2021-01-04 13:01

    I had the same question at my study. Thanks for the info about if and else. Would be my choice too except the assignment is asking us specificly to use the conditional operators. so basically they're asking us to write it in an unreadable way.

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

    This was mine and its correct. I am wondering though if there is a shorter piece of code (using only the conditional operators.).

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

提交回复
热议问题