• Log in or sign up for an account here: https://www.sythe.org/login

Java Tutorial Pt.3 of many! - Switch tutorial

diabl0

Apprentice

Banned
Joined
Jul 25, 2008
Sythe Gold
0
Messages
902
Hey guys, today We will be learning about switches! Exciting huh!

Okay guys, let's get started. So of course you know the basics, starting out adding your main and etc.

so to start off, as usual we are going to add under our first line this:
public static void main(String args[]){

switch1.jpg

Okay guys, now we have added our main, let's get started with the coding. Now the first thing we are going to want to do is add an interger, so lets make ummm Races (I am sorry this may seem racist, But there is no intention of racism I promise). So under our main let's add:

int race;
race = 1;

switch2.jpg

Okay now we have our race added, lets start the switch. So we are going to add out code to start the switch, then we will list 3 possible choices:

switch (race){
case 1:
System.out.println("You can speak English");
break;
case 2:
System.out.println("You can speak Dutch");
break;
case 3:
System.out.println("You can speak Spanish");
break;

beforefinal.jpg

I forgot to say we were going to be working with breaks also. What breaks do is they stop the code after it checks each line. This way the program will not just run through and exit out immediately.

Now under this code, let's add a default:

default:
System.out.print("I do not know your race");
break;

This is just in case the program does not recognize a race, it will go to a default form.

finalvx.jpg


Now, Run the program, it will say you can now speak English. Just change the race number to 2, 3, 4 and see the results.

I hope you guys have learned alot. :) Thank you for reading, make sure you leave comments.
 
Looks like we have a talented Java programmer on Sythe? :P
 
Runescape Gamblingrunescape gambling
Back
Top