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

如何使用选择来验证java类中的多个值?,java,class,constructor,boolean,selection,Java,Class,Constructor,Boolean,Selection,我正在用java开发一个虚拟狗类。狗应该增加和/或减少某些属性,如卫生、快乐和能量,基于某些行为,如进食、清洁和玩耍。所有的狗的属性应该在1-100范围内(这是我有问题弄清楚)。如果超出该范围,则应播放警告限制消息。 在此之前,我尝试过以下代码和更多代码,但我似乎总是遇到一些错误,比如负数 这是我的完整代码,请忽略main类和main方法,并查看VirtualSet类: import java.util.Scanner; public class VirtualPetProgram {

我正在用java开发一个虚拟狗类。狗应该增加和/或减少某些属性,如卫生、快乐和能量,基于某些行为,如进食、清洁和玩耍。所有的狗的属性应该在1-100范围内(这是我有问题弄清楚)。如果超出该范围,则应播放警告限制消息。 在此之前,我尝试过以下代码和更多代码,但我似乎总是遇到一些错误,比如负数

这是我的完整代码,请忽略main类和main方法,并查看VirtualSet类:

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;
        }
    }
}
import java.util.Scanner;
公共类虚拟教育计划{
公共静态void main(字符串[]args){
//初始化扫描仪
扫描仪输入=新扫描仪(System.in);
int选项;
//启动用户体验
System.out.println(“欢迎使用虚拟宠物程序!”);
System.out.print(“您想给您的宠物取什么名字?”);
VirtualPet=新的VirtualPet(input.nextLine());
做{
System.out.println(“\n--------------------------------------------------------------------------------------”;
System.out.println(“请为您选择的选项输入整数:”;
System.out.println(“1.检查状态”);
System.out.println(“2.喂养您的虚拟宠物”);
System.out.println(“3.玩你的虚拟宠物”);
System.out.println(“4.清理虚拟宠物”);
System.out.println(“5.结束程序”);
System.out.print(“\n您的选择:”);
//从用户处获取选择。
option=input.nextInt();
开关(选件){
案例1://检查状态
//使用Getter方法检索值。
System.out.println(“\n值代表”+pet.getName());
System.out.println(“幸福:+pet.getHappiness());
System.out.println(“能量:+pet.getEnergy());
System.out.println(“Hygiene:+pet.getHygiene());
打破
案例2://喂养您的虚拟宠物
//调用feed()实例方法。VirtualSet的feed()方法应该完成所有工作。
if(pet.feed()){
System.out.println(“\n您输入了”+pet.getName()+”);
}否则{
System.out.println(“\n由于限制,您无法馈送”+pet.getName()+”);
}
打破
案例3://玩你的虚拟宠物
//调用play()实例方法。VirtualSet的play()方法应该完成所有工作。
if(pet.play()){
System.out.println(“\n您玩过“+pet.getName()+”);
}否则{
System.out.println(“\n由于限制,您无法使用“+pet.getName()+”);
}
打破
案例4://清洁您的虚拟宠物
//调用clean()实例方法。VirtualSet的clean()方法应该完成所有工作。
if(pet.clean()){
System.out.println(“\n已清理”+pet.getName()+”);
}否则{
System.out.println(“\n由于限制,您无法清除”+pet.getName()+”);
}
打破
案例5://结束方案
//根据幸福感的高低显示摘要。
System.out.println(“感谢您的参与!这里是您宠物的经验总结:”;
如果(pet.getHappiness()>=100){
System.out.println(“你做得很好!你的宠物爱你!”);
}else if(pet.getHappiness()>=80){
System.out.println(“你做得很好!你的宠物喜欢你。”);
}else if(pet.getHappiness()>=60){
System.out.println(“你做得很好,你的宠物并没有它可能的那么高兴。”);
}否则{
System.out.println(“你本可以做得更好,你的宠物不太开心。”);
}
打破
默认值://用户选择了无效选项。
System.out.println(“\n请选择一个有效选项。”);
}
}while(选项!=5);
}
}
类虚拟集合{
//属性应以以下值开头:
私人幸福指数=25;
私人卫生=50;
私人国际能源=25;
私有字符串名称;
公共静态最终字符串默认值_NAME=“Jackie”;
//构造器
公共虚拟网(int新幸福、int新卫生、int新能源){
幸福=新的幸福;
卫生=新卫生;
能源=新能源;
}
//名称的构造函数
公共VirtualSet(字符串newName){
name=newName;
}
//getter和setter方法来保持狗的
//名称少于30个字符,否则将被删除
//调用默认名称。
//我尝试对setter使用单独的方法
//但由于某种原因,它没有起作用。
//如果你对这个问题有什么想法,我将不胜感激。
公共字符串getName(){
if(name.length()<30){
返回名称;
}否则{
名称=默认名称;
}
返回名称;
}
公共幸福指数{
回归幸福;
}
公共幸福(int newHappiness){
幸福=新的幸福;
}
公共卫生(){
返回卫生;
}
公共卫生(国际新卫生){
hy
    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;
        }
    }
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;
    }