封装1月16号
Posted cheng1994
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装1月16号相关的知识,希望对你有一定的参考价值。
package com.fengzhuang; public class Penjuin { private String name; private String sex; private int health ; private int intimacy; private String leibie; public void setHealth(int health) { if(health<0 ||health>100) { System.out.println("你的输入有误,请输入0-100的数,默认值为60"); health= 60; } this.health = health; } public int getHealth() { return this.health; } public void setName(String name ) { this.name= name; } public String getName(){ return this.name; } public void setSex(String sex) { this.sex = sex; } public String getSex() { return this.sex; } public void setIntimacy(int intimacy) { this.intimacy = intimacy; } public int getIntimacy() { return this.intimacy; } public void setLeibie(String leibie) { this.leibie = leibie; } public String getLeibie() { return this.leibie; } public void print() { System.out.println( "宠物的自白"+" "+"我的名字叫"+this.name+",健康值是"+health+"," + "和主人的亲密度是"+intimacy+",我的性别是"+this.sex); } }
package com.fengzhuang; import java.util.Scanner; public class Test { public static void main(String[] args) { Penjuin qq = new Penjuin(); System.out.println("欢迎来到宠物店"); Scanner input = new Scanner(System.in); System.out.println("请输入要领养宠物的名字"); String name = input.nextLine(); qq.setName(name); System.out.println("请选择要领养的宠物类型:(1.狗狗 2.企鹅)"); int chooseLei = input.nextInt(); String leibie = null; if(chooseLei==1) { leibie ="狗狗"; }else if(chooseLei==2) { leibie = "企鹅"; } qq.setLeibie(leibie); System.out.println("请选择"+leibie+"的性别:(1.q仔 2.q妹)"); int chooseSex = input.nextInt(); String sex = null; if(chooseLei==1) { sex ="q仔"; }else if(chooseLei==2) { sex = "q妹"; } qq.setSex(sex); // System.out.println("请输入"+leibie+"的健康值(1~100之间):"); // int health = input.nextInt(); // qq.setHealth(health); qq.print(); } }
以上是关于封装1月16号的主要内容,如果未能解决你的问题,请参考以下文章