Java Tutorials 18 - Ternary Operators

preview_player
Показать описание
This video explains the need of ternary operators and how to use for providing conditional based programming.

Assignment :

Java quick primer and basics for anyone who wants to learn java whether students or professionals, experienced or laterals.
Рекомендации по теме
Комментарии
Автор

The 2nd program for max with if and else condition is as follows:
import java.util.*;
public class Mynewprog {

public static void main(String a1[])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter the 1st no: ");
int n1 = sc.nextInt();
System.out.println("Enter the 2nd no: ");
int n2 = sc.nextInt();
if
(n1>n2) {
System.out.println("max of n1 and n2 is" +n1);}
else
{ System.out.println("max of n1 and n2 is" +n2);
}

}
}

pranavpugaonkar
Автор

I tired an If else condition on the age aspect and it works too!
Hope this helps!
import java.util.*;
public class Mynewprog {

public static void main(String a1[])
{
Scanner sc = new Scanner(System.in);
System.out.println("What is the users age?");
int age = sc.nextInt();

if
(age>=18)
{ System.out.println("Can vote");}
else
{ System.out.println("cannot vote");
}
}

pranavpugaonkar