Sunday, November 11, 2018

Lab5

Assalamualaikum, in this entry i would like to share about lab5 only. this week i only finish my lab5 . the entire three hours, i spend to finish my lab5 only. lab5 is about the switch statement, logical operator and conditional operator.

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");
}

}
}

from the example, we need the command from the keyboard which is number 1 to 12, when the number is selected, it will print out the result based on the month assigned for the umber selected. if the number is out of the range, it will print out '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