Question

I cannot find valid syntax for java shorthand.

How should i rewrite in shorthand this thing:

     int a = 2, b=4;
        if(a < 4) {
        System.out.println("a < b");}
    else { System.out.println("a >= b");
  }
Était-ce utile?

La solution

The "shortest hand" I can imagine is something like:

 String s = (a < 4) ? "a<b" : "a>=b";
 System.out.println(s);

Autres conseils

I suspect you're after the conditional operator:

System.out.println(a < b ? "a < b" : "a >= b");

If that's not what you were looking for, please clarify.

System.out.println("a"+((a<b)?"<":">=")+"b")
int a = 2, b=4;
System.out.println((a < 4) ? "a < b" : "a>=b");

Just for sake of answer, another version of short-hand

System..out.println("a " + (a < 4 ? "<" : ">=") + " b");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top