diff options
author | Cameron Schermerhorn <cameron.schermerhorn@its.ny.gov> | 2015-10-07 08:53:09 -0400 |
---|---|---|
committer | Cameron Schermerhorn <cameron.schermerhorn@its.ny.gov> | 2015-10-07 08:53:09 -0400 |
commit | ec125fb6e793f54645a6ee592e4b5dec85f22cc5 (patch) | |
tree | e821b691e0878177aeea160c9aa01dcbdd6e8ba0 /java.html.markdown | |
parent | 5c677e8071291520297ef3d5d8374c6d11285744 (diff) |
Add example for switching Strings
+ Added switch structure to demonstrate how to switch Strings
+ Added name to contributor list
Diffstat (limited to 'java.html.markdown')
-rw-r--r-- | java.html.markdown | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/java.html.markdown b/java.html.markdown index 478ec683..a862d294 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -5,6 +5,7 @@ contributors: - ["Jakukyo Friel", "http://weakish.github.io"] - ["Madison Dickson", "http://github.com/mix3d"] - ["Simon Morgan", "http://sjm.io/"] + - ["Cameron Schermerhorn", "http://github.com/cschermerhorn"] filename: LearnJava.java --- @@ -275,6 +276,23 @@ public class LearnJava { break; } System.out.println("Switch Case Result: " + monthString); + + //Starting in Java 7 and above, switching Strings works like this: + String myAnswer = "maybe"; + switch(myAnswer){ + case "yes": + System.out.prinln("You answered yes."); + break; + case "no": + System.out.println("You answered no."); + break; + case "maybe": + System.out.println("You answered maybe."); + break; + default: + Sustem.out.println("You answered " + myAnswer); + break; + } // Conditional Shorthand // You can use the '?' operator for quick assignments or logic forks. |