mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Add example for switching Strings
+ Added switch structure to demonstrate how to switch Strings + Added name to contributor list
This commit is contained in:
parent
5c677e8071
commit
ec125fb6e7
@ -5,6 +5,7 @@ contributors:
|
|||||||
- ["Jakukyo Friel", "http://weakish.github.io"]
|
- ["Jakukyo Friel", "http://weakish.github.io"]
|
||||||
- ["Madison Dickson", "http://github.com/mix3d"]
|
- ["Madison Dickson", "http://github.com/mix3d"]
|
||||||
- ["Simon Morgan", "http://sjm.io/"]
|
- ["Simon Morgan", "http://sjm.io/"]
|
||||||
|
- ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]
|
||||||
filename: LearnJava.java
|
filename: LearnJava.java
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -275,6 +276,23 @@ public class LearnJava {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
System.out.println("Switch Case Result: " + monthString);
|
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
|
// Conditional Shorthand
|
||||||
// You can use the '?' operator for quick assignments or logic forks.
|
// You can use the '?' operator for quick assignments or logic forks.
|
||||||
|
Loading…
Reference in New Issue
Block a user