我正在用java开发一个虚拟狗类。狗应该增加和/或减少某些属性,如卫生,快乐和能源的基础上,某些行动,如吃饭,清洁,玩耍。所有的狗的属性应该在1-100范围内(这是我有问题弄清楚)。如果超出该范围,则应播放警告限制消息。在此之前,我尝试过以下代码和更多代码,但我似乎总是出错,比如负数。
这是我的完整代码,请忽略main类和main方法,看看virtualpet类:
import java.util.Scanner;
public class VirtualPetProgram {
public static void main(String[] args) {
// Initialize the Scanner
Scanner input = new Scanner(System.in);
int option;
// Start the user experience
System.out.println("Welcome to the Virtual Pet Program!");
System.out.print("What would you like to name your pet? ");
VirtualPet pet = new VirtualPet(input.nextLine());
do {
System.out.println("\n-----------------------------------------------------------------");
System.out.println("Please enter the integer for the option you choose:");
System.out.println(" 1. Check statuses");
System.out.println(" 2. Feed your virtual pet");
System.out.println(" 3. Play with your virtual pet");
System.out.println(" 4. Clean your virtual pet");
System.out.println(" 5. End program");
System.out.print("\nYour choice: ");
// Get the choice from the user.
option = input.nextInt();
switch (option) {
case 1: // Check statuses
// Retrieve the values using the Getter methods.
System.out.println("\nValues for " + pet.getName());
System.out.println(" Happiness: " + pet.getHappiness());
System.out.println(" Energy: " + pet.getEnergy());
System.out.println(" Hygiene: " + pet.getHygiene());
break;
case 2: // Feed your virtual pet
// Call feed() instance method. VirtualPet's feed() method should be doing all the work.
if (pet.feed()) {
System.out.println("\nYou fed " + pet.getName() + ".");
} else {
System.out.println("\nYou couldn't feed " + pet.getName() + " due to a restriction.");
}
break;
case 3: // Play with your virtual pet
// Call play() instance method. VirtualPet's play() method should be doing all the work.
if (pet.play()) {
System.out.println("\nYou played with " + pet.getName() + ".");
} else {
System.out.println("\nYou couldn't play with " + pet.getName() + " due to a restriction.");
}
break;
case 4: // Clean your virtual pet
// Call clean() instance method. VirtualPet's clean() method should be doing all the work.
if (pet.clean()) {
System.out.println("\nYou cleaned " + pet.getName() + ".");
} else {
System.out.println("\nYou couldn't clean " + pet.getName() + " due to a restriction.");
}
break;
case 5: // End program
// Display a summary depending on how high the happiness is.
System.out.println("Thank you for playing! Here is a summary of your pet's experience:");
if (pet.getHappiness() >= 100) {
System.out.println(" You did a PERFECT job! Your pet loves you!");
} else if (pet.getHappiness() >= 80) {
System.out.println(" You did pretty well! Your pet likes you.");
} else if (pet.getHappiness() >= 60) {
System.out.println(" You did okay. Your pet isn't as happy as it could be.");
} else {
System.out.println(" You could have done a lot better. Your pet isn't very happy.");
}
break;
default: // User selected an invalid option.
System.out.println("\nPlease select a valid option.");
}
} while (option != 5);
}
}
class VirtualPet {
//the attributes should start with the following values:
private int happiness = 25;
private int hygiene = 50;
private int energy = 25;
private String name;
public static final String DEFAULT_NAME = "Jackie";
//the constructor
public VirtualPet(int newHappiness, int newHygiene, int newEnergy) {
happiness = newHappiness;
hygiene = newHygiene;
energy = newEnergy;
}
// the constructor for the name
public VirtualPet(String newName) {
name = newName;
}
//getter and setter method to keep the dog's
//name below 30 characters otherwise it
//invokes the default name.
// I tried to use a separate method for setter
// but for some reason, it didn't work.
//if you have ideas about this issue, I would appreciate it.
public String getName() {
if (name.length() < 30) {
return name;
} else {
name = DEFAULT_NAME;
}
return name;
}
public int getHappiness() {
return happiness;
}
public void setHappiness(int newHappiness) {
happiness = newHappiness;
}
public int getHygiene() {
return hygiene;
}
public void setHygiene(int newHygiene) {
hygiene = newHygiene;
}
public int getEnergy() {
return energy;
}
public void setEnergy(int newEnergy) {
energy = newEnergy;
}
// This is where I set up a boolean method
//to return true and increase both happiness
//and energy if the energy is less than 80.
//the upgrade method called here should work
//as validation for the range of the
//attributes between 1-100.
public boolean feed() {
upgrade();
if (energy < 80) {
happiness += 5;
energy += 30;
} else {
return false;
}
return true;
}
public boolean play() {
upgrade();
if (energy > 30) {
happiness += 20;
energy -= 15;
hygiene -= 30;
} else {
return false;
}
return true;
}
public boolean clean() {
upgrade();
if (energy < 70) {
happiness -= 20;
hygiene += 50;
} else {
return false;
}
return true;
}
// This is the method to validate
//the range of the attributes from 1-100.
//I can't see where or what I am doing wrong
public boolean upgrade() {
if (happiness > 0 && happiness < 100) {
return true;
}
if (energy > 0 && energy < 100){
return true;
}
if (hygiene > 0 && hygiene < 100){
return true;
}
else {
return false;
}
}
}
1条答案
按热度按时间szqfcxe21#
我不懂升级的方法。它返回一个布尔值,表示属性是否在范围内,但您从不使用该方法的输出。您可以修改更新属性的方法,如下所示:
通过这种方式,在更新属性之后,运行upgrade()方法,该方法检查属性的值,并将正确的布尔值返回给switch case。但是,您仍将以这种方式使属性超出范围。解决这个问题的一个愚蠢方法是修改update方法,如下所示: