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"); } }
2 ответов
+ 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.
+ 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