如何使用选择来验证 java class 中的多个值?
How to use selection to validate multiple values in java class?
我正在 java 开发虚拟狗 class。狗应该增加 and/or 减少某些属性,例如卫生、快乐和基于某些行为(例如进食、清洁和玩耍)的能量。狗的所有属性都应该在 1-100 的范围内(这是我搞不清楚的地方)。如果超出该范围,则应播放警告限制消息。
我已经尝试了以下代码以及在此之前的更多代码,但我似乎总是出错,比如大部分是负数。
这是我的完整代码,请忽略mainclass和main方法,看看VirtualPetclass:
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;
}
}
}
我不懂升级方法。它 returns 一个布尔值,表示属性是否在范围内,但您永远不会使用该方法的输出。
您可以像这样修改更新属性的方法:
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() 方法和 returns 开关盒的正确布尔值。
但是,您仍然会以这种方式获得超出范围的属性。解决这个问题的一种愚蠢的方法是也修改更新方法如下:
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;
}
我正在 java 开发虚拟狗 class。狗应该增加 and/or 减少某些属性,例如卫生、快乐和基于某些行为(例如进食、清洁和玩耍)的能量。狗的所有属性都应该在 1-100 的范围内(这是我搞不清楚的地方)。如果超出该范围,则应播放警告限制消息。 我已经尝试了以下代码以及在此之前的更多代码,但我似乎总是出错,比如大部分是负数。
这是我的完整代码,请忽略mainclass和main方法,看看VirtualPetclass:
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;
}
}
}
我不懂升级方法。它 returns 一个布尔值,表示属性是否在范围内,但您永远不会使用该方法的输出。 您可以像这样修改更新属性的方法:
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() 方法和 returns 开关盒的正确布尔值。 但是,您仍然会以这种方式获得超出范围的属性。解决这个问题的一种愚蠢的方法是也修改更新方法如下:
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;
}