first, switch statement is something like the if-else statement, just the way to write it is different but the output is the same. switch statement also need to use scanner as we need to get the command from the keyboard. the example of switch statement is like this:
import java.util.Scanner ;
public class Month {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.print("Enter number 1 until 12 only :");
int x = input.nextInt();
switch(x) {
case 1 : System.out.println("january");
break;
case 2 : System.out.println("february");
break;
case 3 : System.out.println("march");
break;
case 4 : System.out.println("april");
break;
case 5 : System.out.println("may");
break;
case 6 : System.out.println("june");
break;
case 7 : System.out.println("july");
break;
case 8 : System.out.println("august");
break;
case 9 : System.out.println("september");
break;
case 10 : System.out.println("october");
break;
case 11 : System.out.println("november");
break;
case 12 : System.out.println("december");
break;
default : System.out.println("error");
}
}
}
next is about conditional operator combine with Boolean expression. for example for this case is like:
ticketPrice = (ages >= 6) ? 20 : 10
from the example we can know rhat if the ages is more than or equal to 6 years old, the ticket price is RM 20 or else if ages is less than 6 years old, the ticket price will be RM 10.
that is all for this entry. thank you.
p/s: link to Atika`s blog
ssk3100.blogspot.com
No comments:
Post a Comment