如何使用选择来验证java类中的多个值?

tjvv9vkg  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(288)

我正在用java开发一个虚拟狗类。狗应该增加和/或减少某些属性,如卫生,快乐和能源的基础上,某些行动,如吃饭,清洁,玩耍。所有的狗的属性应该在1-100范围内(这是我有问题弄清楚)。如果超出该范围,则应播放警告限制消息。在此之前,我尝试过以下代码和更多代码,但我似乎总是出错,比如负数。
这是我的完整代码,请忽略main类和main方法,看看virtualpet类:

  1. import java.util.Scanner;
  2. public class VirtualPetProgram {
  3. public static void main(String[] args) {
  4. // Initialize the Scanner
  5. Scanner input = new Scanner(System.in);
  6. int option;
  7. // Start the user experience
  8. System.out.println("Welcome to the Virtual Pet Program!");
  9. System.out.print("What would you like to name your pet? ");
  10. VirtualPet pet = new VirtualPet(input.nextLine());
  11. do {
  12. System.out.println("\n-----------------------------------------------------------------");
  13. System.out.println("Please enter the integer for the option you choose:");
  14. System.out.println(" 1. Check statuses");
  15. System.out.println(" 2. Feed your virtual pet");
  16. System.out.println(" 3. Play with your virtual pet");
  17. System.out.println(" 4. Clean your virtual pet");
  18. System.out.println(" 5. End program");
  19. System.out.print("\nYour choice: ");
  20. // Get the choice from the user.
  21. option = input.nextInt();
  22. switch (option) {
  23. case 1: // Check statuses
  24. // Retrieve the values using the Getter methods.
  25. System.out.println("\nValues for " + pet.getName());
  26. System.out.println(" Happiness: " + pet.getHappiness());
  27. System.out.println(" Energy: " + pet.getEnergy());
  28. System.out.println(" Hygiene: " + pet.getHygiene());
  29. break;
  30. case 2: // Feed your virtual pet
  31. // Call feed() instance method. VirtualPet's feed() method should be doing all the work.
  32. if (pet.feed()) {
  33. System.out.println("\nYou fed " + pet.getName() + ".");
  34. } else {
  35. System.out.println("\nYou couldn't feed " + pet.getName() + " due to a restriction.");
  36. }
  37. break;
  38. case 3: // Play with your virtual pet
  39. // Call play() instance method. VirtualPet's play() method should be doing all the work.
  40. if (pet.play()) {
  41. System.out.println("\nYou played with " + pet.getName() + ".");
  42. } else {
  43. System.out.println("\nYou couldn't play with " + pet.getName() + " due to a restriction.");
  44. }
  45. break;
  46. case 4: // Clean your virtual pet
  47. // Call clean() instance method. VirtualPet's clean() method should be doing all the work.
  48. if (pet.clean()) {
  49. System.out.println("\nYou cleaned " + pet.getName() + ".");
  50. } else {
  51. System.out.println("\nYou couldn't clean " + pet.getName() + " due to a restriction.");
  52. }
  53. break;
  54. case 5: // End program
  55. // Display a summary depending on how high the happiness is.
  56. System.out.println("Thank you for playing! Here is a summary of your pet's experience:");
  57. if (pet.getHappiness() >= 100) {
  58. System.out.println(" You did a PERFECT job! Your pet loves you!");
  59. } else if (pet.getHappiness() >= 80) {
  60. System.out.println(" You did pretty well! Your pet likes you.");
  61. } else if (pet.getHappiness() >= 60) {
  62. System.out.println(" You did okay. Your pet isn't as happy as it could be.");
  63. } else {
  64. System.out.println(" You could have done a lot better. Your pet isn't very happy.");
  65. }
  66. break;
  67. default: // User selected an invalid option.
  68. System.out.println("\nPlease select a valid option.");
  69. }
  70. } while (option != 5);
  71. }
  72. }
  73. class VirtualPet {
  74. //the attributes should start with the following values:
  75. private int happiness = 25;
  76. private int hygiene = 50;
  77. private int energy = 25;
  78. private String name;
  79. public static final String DEFAULT_NAME = "Jackie";
  80. //the constructor
  81. public VirtualPet(int newHappiness, int newHygiene, int newEnergy) {
  82. happiness = newHappiness;
  83. hygiene = newHygiene;
  84. energy = newEnergy;
  85. }
  86. // the constructor for the name
  87. public VirtualPet(String newName) {
  88. name = newName;
  89. }
  90. //getter and setter method to keep the dog's
  91. //name below 30 characters otherwise it
  92. //invokes the default name.
  93. // I tried to use a separate method for setter
  94. // but for some reason, it didn't work.
  95. //if you have ideas about this issue, I would appreciate it.
  96. public String getName() {
  97. if (name.length() < 30) {
  98. return name;
  99. } else {
  100. name = DEFAULT_NAME;
  101. }
  102. return name;
  103. }
  104. public int getHappiness() {
  105. return happiness;
  106. }
  107. public void setHappiness(int newHappiness) {
  108. happiness = newHappiness;
  109. }
  110. public int getHygiene() {
  111. return hygiene;
  112. }
  113. public void setHygiene(int newHygiene) {
  114. hygiene = newHygiene;
  115. }
  116. public int getEnergy() {
  117. return energy;
  118. }
  119. public void setEnergy(int newEnergy) {
  120. energy = newEnergy;
  121. }
  122. // This is where I set up a boolean method
  123. //to return true and increase both happiness
  124. //and energy if the energy is less than 80.
  125. //the upgrade method called here should work
  126. //as validation for the range of the
  127. //attributes between 1-100.
  128. public boolean feed() {
  129. upgrade();
  130. if (energy < 80) {
  131. happiness += 5;
  132. energy += 30;
  133. } else {
  134. return false;
  135. }
  136. return true;
  137. }
  138. public boolean play() {
  139. upgrade();
  140. if (energy > 30) {
  141. happiness += 20;
  142. energy -= 15;
  143. hygiene -= 30;
  144. } else {
  145. return false;
  146. }
  147. return true;
  148. }
  149. public boolean clean() {
  150. upgrade();
  151. if (energy < 70) {
  152. happiness -= 20;
  153. hygiene += 50;
  154. } else {
  155. return false;
  156. }
  157. return true;
  158. }
  159. // This is the method to validate
  160. //the range of the attributes from 1-100.
  161. //I can't see where or what I am doing wrong
  162. public boolean upgrade() {
  163. if (happiness > 0 && happiness < 100) {
  164. return true;
  165. }
  166. if (energy > 0 && energy < 100){
  167. return true;
  168. }
  169. if (hygiene > 0 && hygiene < 100){
  170. return true;
  171. }
  172. else {
  173. return false;
  174. }
  175. }
  176. }
szqfcxe2

szqfcxe21#

我不懂升级的方法。它返回一个布尔值,表示属性是否在范围内,但您从不使用该方法的输出。您可以修改更新属性的方法,如下所示:

  1. public boolean feed() {
  2. if (energy < 80) {
  3. happiness += 5;
  4. energy += 30;
  5. }
  6. return upgrade();
  7. }
  8. public boolean play() {
  9. if (energy > 30) {
  10. happiness += 20;
  11. energy -= 15;
  12. hygiene -= 30;
  13. {
  14. return upgrade();
  15. }
  16. public boolean clean() {
  17. if (energy < 70) {
  18. happiness -= 20;
  19. hygiene += 50;
  20. {
  21. return upgrade();
  22. }
  23. // This is the method to validate
  24. //the range of the attributes from 1-100.
  25. //I can't see where or what I am doing wrong
  26. public boolean upgrade() {
  27. if (happiness > 0 && happiness < 100) {
  28. return true;
  29. }
  30. if (energy > 0 && energy < 100){
  31. return true;
  32. }
  33. if (hygiene > 0 && hygiene < 100){
  34. return true;
  35. }
  36. else {
  37. return false;
  38. }
  39. }

通过这种方式,在更新属性之后,运行upgrade()方法,该方法检查属性的值,并将正确的布尔值返回给switch case。但是,您仍将以这种方式使属性超出范围。解决这个问题的一个愚蠢方法是修改update方法,如下所示:

  1. public boolean upgrade() {
  2. boolean isInRange=true;
  3. if(happiness>100){
  4. happiness=100;
  5. isInRange=false;
  6. }
  7. if(happiness<0){
  8. happiness=0;
  9. isInRange=false;
  10. }
  11. if(energy>100){
  12. energy=100;
  13. isInRange=false;
  14. }
  15. if(energy<0){
  16. energy=0;
  17. isInRange=false;
  18. }
  19. if(hygiene>100){
  20. hygiene=100;
  21. isInRange=false;
  22. }
  23. if(hygiene<0){
  24. hygiene=0;
  25. isInRange=false;
  26. }
  27. return isInRange;
  28. }
展开查看全部

相关问题