JNET

Challenge 3 Java Programming 2017

Sep 25th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. public class Calendar
  2. {
  3. public static void main(String [] args)
  4. {
  5. System.out.println(" Sun Mon Tue Wed Thu Fri Sat ");
  6. headers();
  7. cal(31, 6);
  8. headers();
  9. }
  10. public static void headers()
  11. {
  12. System.out.println("+------+------+------+------+------+------+------+");
  13. }
  14. public static String padded(int n, int width)
  15. {
  16. String s= "" + n;
  17. for (int i = s.length(); i < width; i++)
  18. {
  19. s = " " + s;
  20. }
  21. return s;
  22. }
  23. public static void cal(int days, int sun)
  24. {
  25. int printdays = 1;
  26. int weekday = 1;
  27. while(printdays <= days)
  28. {
  29. if (weekday == (8 - sun) % 7 + 1 && printdays == 1)
  30. {
  31. System.out.print("|" + padded(printdays, 6));
  32. printdays++;
  33. }
  34. else if(weekday < (8 - sun) % 7 + 1 && printdays == 1)
  35. {
  36. System.out.print("| ");
  37. }
  38. else if(printdays > 1)
  39. {
  40. System.out.print("|" + padded(printdays, 6));
  41. printdays++;
  42. }
  43. weekday++;
  44. if(weekday > 7)
  45. {
  46. System.out.println("|");
  47. weekday = 1;
  48. }
  49. }
  50. if(weekday != 1)
  51. {
  52. for(; weekday <= 7; weekday++)
  53. {
  54. System.out.print("| ");
  55. }
  56. System.out.println("|");
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment