当前位置 : 主页 > 手机开发 > 其它 >

用封装实现错误的赋值而导致输出的信息不符合实际

来源:互联网 收集:自由互联 发布时间:2021-06-19
package Month.JUL.JUL11;import java.util.Scanner; public class QiEr { private String name; private int xingBie; private int health; private int love; public String getName() { return name; } public void setName(String name) { this .name = n
package Month.JUL.JUL11;

import java.util.Scanner;

public class QiEr {
    private String name;
    private int xingBie;
    private int health;
    private int love;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getXingBie() {
        return xingBie;
    }

    public void setXingBie(int xingBie) {
        if (xingBie == 1 || xingBie == 2) {
            this.xingBie = xingBie;
        } else {
            System.out.println("您选择的性别不在该范围内!");
            this.xingBie = 1;
        }
    }

    static Scanner input = new Scanner(System.in);

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        if (health > 0 && health < 100) {
            this.health = health;
        } else {
            this.health = 60;
            System.out.println("输出信息错误,默认使用初始值!");
        }
    }

    public int getLove() {
        return love;
    }

    public void setLove(int love) {
        if (love > 0 && love < 100) {
            this.love = love;
        } else {
            this.love = 60;
            System.out.println("输出信息错误,默认使用初始值!");
        }
    }

    public static void main(String[] args) {
        QiEr qi = new QiEr();
        System.out.println("请输入企鹅名称:");
        qi.setName(input.next());
        System.out.println("请选择企鹅性别(1、Q仔,2、Q妹):");
        qi.setXingBie(input.nextInt());
        String xingBie = "";
        if (qi.getXingBie() == 1) {
            xingBie = "Q仔";
        } else {
            xingBie = "Q妹";
        }
        System.out.println("请输入健康值:");
        qi.setHealth(input.nextInt());
        System.out.println("请输入亲密度:");
        qi.setLove(input.nextInt());

        System.out
                .println("企鹅名称:" + qi.getName() + ",性别:" + xingBie + ",健康值:" + qi.getHealth() + "亲密度:" + qi.getLove());
    }

}

网友评论