Home arrow Java & J2EE arrow Page 3 - Conditionals, Expressions and Other Java Operators

Climbing the Old If Else If Ladder - Java

In our last article we left off with Operators. Since then a strange time fluctuation has occurred, and that is exactly where we pick up. So keep reading to learn about exotic things like conditionals, expressions, and more.

TABLE OF CONTENTS:
  1. Conditionals, Expressions and Other Java Operators
  2. Expressions
  3. Climbing the Old If Else If Ladder
  4. The Switch Statement
By: James Payne
Rating: starstarstarstarstar / 3
October 23, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Since the beginning of time man has been asking these important questions: Can a homo sapien beat up a homo erectus? Can a man beat a saber-toothed tiger? Can a saber-toothed tiger defeat a giant sloth? Will Jessie Jackson ever become president? Did Bill Clinton ever have relations with that woman? 

When you have a lot of Ifs, you need to build yourself a good old fashioned If Else If statement. Look at the beautiful and pertinent one below:


class SuperHero {

public static void main(String[]) {

int hulk = 9;

int daredevil = 10;

int batman = 11;

int wolverine = 8;

String winner = "Whoever"

 

 

if(daredevil > hulk && daredevil > batman && daredevil > wolverine)

winner = "Daredevil is the winner!";

else if(hulk > daredevil && hulk > batman && hulk > wolverine)

winner = "The Incredible Hulk is the winner!";

else if(batman > daredevil && batman > hulk && batman > wolverine)

winner = "Batman is the winner!!";

else if(wolverine > hulk && wolverine > daredevil && wolverine > batman)

winner = "Wolverine is the winner!";


else winner = "Nobody wins!";


System.out.println(winner);

 

}

}

The above code is for a battle royal amongst the super heroically inclined. Each hero is assigned a numeric value and then compared to each other hero. Whichever hero has the highest value gets stored in the winner category. In this case, the winner is Batman, and the following prints to the screen:

  Batman is the winner!

If no one met the criteria (had a higher value than the other three super heroes), then the following would have been printed to the screen:

  Nobody wins!



 
 
>>> More Java & J2EE Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: