More Examples of the Ternary Operator In Action:
String ouput="";
output = ( a==10 || a == 20 ) ? "TEN or TWENTY" : "NOT 10 or 20";
// uses short-circuit logical OR operator
String output="";
String name="Barney";
output = ( name.equals("Barney") ) ? "Dinosaur" : "Poodle" ;
// uses String.equals function
boolean short = true;
int output = 0;
output = short ? 100 : 200;
//output assigned 100 since short is true
Assignment:
Write an applet which contains a list of names contained in an array of type
String. Have at least eight names in your list. For each name determine if
it is the name of a DORK or a NERD. At least three people on your list
should be DORKs and at least three people on your list should be NERDs.
Print out a little sentence like this for each person:
Alvin is a DORK.
To create an String array you do the following:
String[] list = new String[]{ "one", "two", "three" };