用封装实现错误的赋值而导致输出的信息不符合实际
Posted zeng1997
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用封装实现错误的赋值而导致输出的信息不符合实际相关的知识,希望对你有一定的参考价值。
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());
以上是关于用封装实现错误的赋值而导致输出的信息不符合实际的主要内容,如果未能解决你的问题,请参考以下文章