what's worng in it?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what's worng in it??

import java.util.*; class Ternary { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); (n%2==0)?System.out.println("1"):System.out.println("0"); } }

9th Jun 2022, 3:34 PM
Shashi Singh
Shashi Singh - avatar
2 Answers
+ 6
People will be more likely to help, if you put your FORMATTED code in a SCRIPT on sololearn playground. The issue is that you are trying to use a ternary operation without assignment.
9th Jun 2022, 3:52 PM
Lisa
Lisa - avatar
+ 4
The ternary operator returns a value. condition ? expression1 : expression2 If the condition is true, it returns expression1 else it returns expression2. int n = sc.nextInt(); String msg = (n%2==0)? "1" : "0"; System.out.println(msg); If n%2 == 0 it returns 1 to msg else it returns 0. https://jenkov.com/tutorials/java/ternary-operator.html https://www.baeldung.com/java-ternary-operator
9th Jun 2022, 3:52 PM
Denise Roßberg
Denise Roßberg - avatar