我正在用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;
}
}
}发布于 2020-12-11 17:46:50
我不明白升级方法的要点。它返回一个布尔值,表明属性是否在范围内,但您从未使用过该方法的输出。您可以像这样修改更新属性的方法:
public boolean feed() {
if (energy < 80) {
happiness += 5;
energy += 30;
}
return upgrade();
}
public boolean play() {
if (energy > 30) {
happiness += 20;
energy -= 15;
hygiene -= 30;
{
return upgrade();
}
public boolean clean() {
if (energy < 70) {
happiness -= 20;
hygiene += 50;
{
return upgrade();
}
// 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;
}
}通过这种方式,在更新属性之后,您可以运行upgrade()方法,该方法检查属性值并将适当的布尔值返回给切换用例。但是,通过这种方式,您仍然会拥有超出范围的属性。解决这个问题的一个愚蠢的方法是修改update方法,如下所示:
public boolean upgrade() {
boolean isInRange=true;
if(happiness>100){
happiness=100;
isInRange=false;
}
if(happiness<0){
happiness=0;
isInRange=false;
}
if(energy>100){
energy=100;
isInRange=false;
}
if(energy<0){
energy=0;
isInRange=false;
}
if(hygiene>100){
hygiene=100;
isInRange=false;
}
if(hygiene<0){
hygiene=0;
isInRange=false;
}
return isInRange;
}https://stackoverflow.com/questions/65248727
复制相似问题