简体   繁体   English

Java:三元运算符,不能正常工作

[英]Java: Ternary operator, not working as should

I have some sample code and it looks like it uses a ternery operator but it takes 3 items. 我有一些示例代码,看起来它使用了ternery运算符,但它需要3个项目。 So I am really confused, the ones I have seen in other languages take only 2, ie true and false. 所以我真的很困惑,我在其他语言中看到的只有2个,即对与错。

Here is the code 这是代码

        Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(PROPERTY_ID)
                : analytics.newTracker(R.xml.ecommerce_tracker);

what i wish to do is remove the line 我想做的是删除行

     : analytics.newTracker(R.xml.ecommerce_tracker);

So it would now be 所以现在

        Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker)
                : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(PROPERTY_ID);

but it states it is missing a : (colon) in the IDE. 但它表示在IDE中缺少:(冒号)。 So maybe this isn't a ternery operator after all ? 那么,这毕竟不是实习生吗?

THe ide I am using is Android Studio. 我正在使用的是Android Studio。

I know it must be something simple, but i can't figure it out. 我知道这一定很简单,但我不知道。

Any ideas ? 有任何想法吗 ?

What you have there is nested ternary operators. 您所拥有的是嵌套的三元运算符。 Think of them as the following (pseudo-code below) 将它们视为以下内容(下面的伪代码)

  condition ? answerifTrue : secondternaryoperator;
  where secondternaryoperator =
        secondcondition ? answerifTrue : answerIfFalse;

Basically, this is two ternary operators combined. 基本上,这是两个三元运算符的组合。 If the first condition is true it simply returns answerifTrue . 如果第一个条件为true,则仅返回answerifTrue But if the condition is false, it runs the second ternary operator to determine what to return based on a second condition. 但是,如果条件为假,它将运行第二个三元运算符,以根据第二个条件确定要返回的内容。 A singular ternary operator simply looks like this; 奇异的三元运算符看起来就是这样。

  condition ? answerifTrue : answerIfFalse;        

So if you want to create a single ternary operator from both you need to determine which answer you want if the first condition is false, for example; 因此,如果您要从两者创建单个三元运算符,则需要确定在第一个条件为假的情况下要使用哪个答案;

  Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker) : analytics.newTracker(PROPERTY_ID);

I imagine however, that what you need isn't well suited to ternary operators anyway and you might need to restructure the statement entirely into if else blocks or a switch statement. 但是我想,您需要的东西无论如何都不适合三元运算符,并且您可能需要将语句完全重组为if else块或switch语句。

Hm, as far as I understand that use case you have to do something like this: 嗯,据我所知,用例必须执行以下操作:

 Tracker t = null; 

 if (trackerId == TrackerName.APP_TRACKER) {
    t = analytics.newTracker(R.xml.app_tracker);
 } else if (trackerId == TrackerName.GLOBAL_TRACKER) {
    t = analytics.newTracker(PROPERTY_ID);
 } else {
    t = analytics.newTracker(R.xml.ecommerce_tracker);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM