华恩JAVA班第21天

华恩JAVA班第21天

电子宠物

 


public class Dog {
 //狗的属性
 private String name;
 private int health;
 private int love = 20;
 private String species;
 
 //构造函数对狗进行初始化
 public Dog(String name,String species){
  this.name = name;
  this.species = species;
 }
 public void print(){
  System.out.println("狗的名称:"+this.name+
        "狗的健康值:"+this.health+
        "狗的亲密值:"+this.love+
        "狗的品种:"+this.species);
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getHealth() {
  return health;
 }
 public boolean setHealth(int health){
  if(health>100 ||health<0){
   returnfalse;
  }else{
   this.health =health;
   returntrue;
  }
 }
 public int getLove() {
  return love;
 }
 public void setLove(int love) {
  this.love = love;
 }
 public String getSpecies() {
  return species;
 }
 public void setStrain(String species) {
  this.species = species;
 }
 public String toString(){
  return    "宠物的名称:"+this.name+"\n"+
      "宠物的健康值:"+this.health+"\n"+
      "宠物的亲密值:"+this.love+"\n"+
      "宠物的品种:"+this.species;
 }
}

 

 

import java.util.Scanner;
public class PetTest {
 public static void main(String[] args) {
  
  Scanner in = newScanner(System.in);
  print("欢迎来到宠物店");
  print("请输入要领养宠物的名字:");
  String name = in.next();
  print("请选择要领养宠物的种类:"+"\n"+"1.狗  "+"\n"+"2.企鹅");
  
  String species=null;
  while(true){
   int c =in.nextInt();
   if(c ==1){
    print("恭喜你,领到一只小狗");
    print("请选择"+name+"的种类:(1.聪明的拉布拉多犬2.酷酷的雪纳瑞)");
    while(true){
     intc1 = in.nextInt();
     if(c1== 1){
      species= "聪明的拉布拉多犬";
      break;
     }elseif(c1 == 2){
      species= "酷酷的雪纳瑞";
      break;
     }else{
      print("输入错误,请重新输入");
     }
    }
    break;
   }else if(c ==2){
    print("恭喜你,领到一只企鹅");
    print("请选择"+name+"的种类:(1.帝企鹅2.岩企鹅)");
    while(true){
     intc1 = in.nextInt();
     if(c1== 1){
      species= "帝企鹅";
      break;
     }elseif(c1 == 2){
      species= "岩企鹅";
      break;
     }else{
      print("输入错误,请重新输入");
     }
    }
    break;
   }else{
    print("输入错误,请重新输入");
   }
  }
  Dog dog = newDog(name,species);
  print("请选择要领养"+species+"的健康值");
  int health =in.nextInt();
  while(!dog.setHealth(health)){
   print("输入错误,请重新输入");
    health =in.nextInt();
  }
  System.out.println(dog);
 }
 public static void print(String a){
  System.out.println(a);
 }
}

 

你可能感兴趣的:(华恩JAVA班,java,java)