Guest User

Untitled

a guest
Apr 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. class PetAdvice
  4. {
  5. public static void main(String args[]) throws Exception
  6. {
  7. int hours;
  8. int a = 0;
  9.  
  10. char house = ' ';
  11.  
  12. String hoursString;
  13.  
  14. String petAdvice = "Hello";
  15.  
  16. System.out.println("H) House");
  17. System.out.println("A) Apartment");
  18. System.out.println("D) Dormitory");
  19. System.out.println("Insert the word according to the type of residence you live in.");
  20.  
  21. house = (char) System.in.read();
  22.  
  23. while ((house != 'H') && (house != 'A') && (house != 'D'))
  24. {
  25. System.out.println("You didn't enter a correct house type insert H, A or D according to the residence.");
  26. house = (char) System.in.read();
  27. System.in.read();
  28. System.in.read();
  29. }
  30.  
  31. hoursString = JOptionPane.showInputDialog("Insert number of hours you spend at your residence");
  32. hours = Integer.parseInt(hoursString);
  33.  
  34. switch(house)
  35. {
  36. case('H'):
  37. {
  38. if(hours > 18)
  39. {
  40. a = 0;
  41. petAdvice = "Pot bellied pig";
  42. break;
  43. }
  44. if ((hours >= 10) && (hours <= 17))
  45. {
  46. a = 0;
  47. petAdvice = "Dog";
  48. break;
  49. }
  50. if ((hours < 10))
  51. {
  52. a = 0;
  53. petAdvice = "Snake";
  54. break;
  55. }
  56.  
  57. }
  58.  
  59. case('A'): {
  60. if (hours >= 10)
  61. {
  62. a = 0;
  63. petAdvice = "Cat";
  64. break;
  65. }
  66. if (hours < 10)
  67. {
  68. a = 0;
  69. petAdvice = "Hamster";
  70. break;
  71. }
  72. }
  73. case('D'):
  74. {
  75. if (hours >= 6)
  76. {
  77. petAdvice = "Fish";
  78. a = 0;
  79. break;
  80. }
  81. if (hours < 6)
  82. {
  83. petAdvice = "Ant farm";
  84. a = 0;
  85. break;
  86. }
  87. }
  88. default:
  89. {
  90. System.out.println("You didn't enter correct information.");
  91. a = 1;
  92. }
  93. }
  94. if (a != 1)
  95. {
  96. JOptionPane.showMessageDialog(null, "You should buy yourself a\n" + petAdvice, "Your pet advice", JOptionPane.INFORMATION_MESSAGE);
  97. }
  98. }
  99. }
Add Comment
Please, Sign In to add comment