Monday, 6 June 2016

Chapter 3 Exercise 4, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**3.4 (Random month) Write a program that randomly generates an integer between 1 and 12 and displays the English month name January, February, …, December for the number 1, 2, …, 12, accordingly.


public class ProgrammingEx3_4 {
 
 public static void main(String[] args) {
  int number1 = (int) (System.currentTimeMillis() % 12 + 1);
  String month = "";
   
  switch(number1){
  case 1: month = "January"; break;
  case 2: month = "February"; break;
  case 3: month = "March"; break;
  case 4: month = "April"; break;
  case 5: month = "May"; break;
  case 6: month = "June"; break;
  case 7: month = "July"; break;
  case 8: month = "August"; break;
  case 9: month = "September"; break;
  case 10: month = "October"; break;
  case 11: month = "November"; break;
  case 12: month = "December"; break;
  }
   
  System.out.println(month);
 
 }
 
}

5 comments :

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice work. Please how do you colour the keywords in your code? Please reply.

    ReplyDelete
    Replies
    1. Thank you for asking the question I use custom css in blogger to highlight the keywords so it seems easier for reader to go through code.

      Delete
    2. Thank you for your reply. Can you please share the code with me? I'd love to use it on my blog too.

      Delete