2019-1-16 封存上机作业
一、领养宠物
企鹅类👇👇👇
1 package com.test;
2 /**
3 * 电子宠物系统
4 * @author Mr.kemi
5 *2019-1-16
6 */
7 public class Pet {
8 private String name;
9 private String sex;
10 private int health;
11 private int love;
12 public String getName() {
13 return name;
14 }
15 public void setName(String name) {
16 this.name = name;
17 }
18 public String getSex() {
19 return sex;
20 }
21 public void setSex(String sex) {
22 if(sex.equals("1")) {
23 this.sex = "Q仔";
24 return;
25 }
26 this.sex = "Q妹";
27 }
28 public int getHealth() {
29 return health;
30 }
31 public void setHealth(int health) {
32 if(health>100 || health<0) {
33 System.out.println("健康是有误,修改为默认值");
34 health = 60;
35 }
36 this.health = health;
37 }
38 public int getLove() {
39 return love;
40 }
41 public void setLove(int love) {
42 if(love>100 || love<0) {
43 System.out.println("亲密度是有误,修改为默认值");
44 love = 60;
45 }
46 this.love = love;
47 }
48 public void print() {
49 System.out.println("宠物的自白");
50 System.out.println("我的名字叫:"+getName()+"健康值是:"+getHealth()+"和主人的亲密度是:"+getLove()+"我的性别是:"+getSex());
51 }
52 }
狗🐕类 👇👇👇
1 package com.test;
2
3 public class Dog {
4 private int health;
5 private int love;
6 private String name;
7 private String type;
8
9
10
11
12 public int getHealth() {
13 return health;
14 }
15 public void setHealth(int health) {
16 if(health>100 || health<0) {
17 System.out.println("亲密度有误,修改默认值");
18 health = 60;
19 }
20 this.health = health;
21 }
22 public int getLove() {
23 return love;
24 }
25 public void setLove(int love) {
26 if(love>100 || love<0) {
27 System.out.println("亲密度有误,修改默认值");
28 love = 60;
29 }
30 this.love = love;
31 }
32 public String getName() {
33 return name;
34 }
35 public void setName(String name) {
36 this.name = name;
37 }
38 public String getType() {
39 return type;
40 }
41 public void setType(String type) {
42 if(type.equals("1")) {
43 this.type = "公仔";
44 return;
45 }
46 this.type = "母仔";
47 }
48 public void print() {
49 System.out.println("宠物的自白");
50 System.out.println("我的名字叫:"+getName()+"健康值是:"+getHealth()+"和主人的亲密度是:"+getLove()+"我的性别是:"+getType());
51 }
52
53 }
主类👇👇👇
1 package com.test;
2
3 import java.util.Scanner;
4
5 public class TestPet {
6 public static void main(String[] args) {
7 Scanner input = new Scanner(System.in);
8 Pet pet = new Pet();
9 Dog dog = new Dog();
10 System.out.println("欢迎来到宠物店");
11 System.out.println("请输入要领养宠物的类型:(1.狗/2.企鹅)");
12 int s = input.nextInt();
13 if(s==2) {
14 System.out.println("请输入要领养宠物的名字:");
15 pet.setName(input.next());
16 System.out.println("请选择宠物的性别:(1.Q仔 2.Q妹)");
17 pet.setSex(input.next());
18 System.out.println("请输入宠物的健康值:(1-100之间)");
19 pet.setHealth(input.nextInt());
20 System.out.println("请输入宠物的亲密度:(1-100之间)");
21 pet.setLove(input.nextInt());
22 pet.print();
23 }else {
24 System.out.println("请输入要领养宠物的名字:");
25 dog.setName(input.next());
26 System.out.println("请选择宠物的性别:(1.公仔 2.母仔)");
27 dog.setType(input.next());
28 System.out.println("请输入宠物的健康值:(1-100之间)");
29 dog.setHealth(input.nextInt());
30 System.out.println("请输入宠物的亲密度:(1-100之间)");
31 dog.setLove(input.nextInt());
32 dog.print();
33 }
34
35
36 }
37 }
运行结果👇👇👇 吃俺老孙一棒🥁