mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-17 12:10:52 +02:00
Merge pull request #1389 from cschermerhorn/master
[java/en] Add Switching on String Example to Java
This commit is contained in:
@@ -6,6 +6,7 @@ contributors:
|
|||||||
- ["Madison Dickson", "http://github.com/mix3d"]
|
- ["Madison Dickson", "http://github.com/mix3d"]
|
||||||
- ["Simon Morgan", "http://sjm.io/"]
|
- ["Simon Morgan", "http://sjm.io/"]
|
||||||
- ["Zachary Ferguson", "http://github.com/zfergus2"]
|
- ["Zachary Ferguson", "http://github.com/zfergus2"]
|
||||||
|
- ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]
|
||||||
filename: LearnJava.java
|
filename: LearnJava.java
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -287,6 +288,23 @@ public class LearnJava {
|
|||||||
}
|
}
|
||||||
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.println("You answered yes.");
|
||||||
|
break;
|
||||||
|
case "no":
|
||||||
|
System.out.println("You answered no.");
|
||||||
|
break;
|
||||||
|
case "maybe":
|
||||||
|
System.out.println("You answered maybe.");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.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.
|
||||||
// Reads as "If (statement) is true, use <first value>, otherwise, use
|
// Reads as "If (statement) is true, use <first value>, otherwise, use
|
||||||
|
Reference in New Issue
Block a user