封裝1月16號
阿新 • • 發佈:2019-01-16
歡迎來到 static util pub new sel name alt bubuko
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( "寵物的自白"+"\t"+"我的名字叫"+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號