
正文
宠物java代码 java宠物类
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java中编写宠物注意事项是什么
加注释。Java是一门面向对象的编程语言,用其编写宠物注意事项加注释即可,当然注释也不要过多,在该加的地方加注释即可(一般是在一些重要变量或是较难理解的方法前添加)。使用多行注释的话还可以在我们调用它的时候给我们一些提示。
相关问答
Q1: 在JAVA中比较两个完全相同的宠物
public class Cat {
private String name;
private int old;
private int weight;
private String color;
public Cat() {
super();
}
public Cat( String name,int old,int weight,String color) {
super();
this.name=name;
this.old=old;
this.weight=weight;
this.color=color;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setOld(int old) {
this.old = old;
}
public int getOld() {
return old;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public String toString() {
return "小猫名字为:"+name+"年龄:"+old+"重量:"+weight+"颜色:"+color;
}
public boolean equals(Object obj) {
if(obj instanceof Cat){
if(((Cat)obj).getName().equals(name) ((Cat)obj).getOld()==old ((Cat)obj).getWeight()==weight ((Cat)obj).getColor().equals(color)){
return true;
}
}
return false;
}
}
public class Test1 {
public Test1() {
super();
}
public static void main(String[] args) {
Cat cat1 = new Cat("小猫1",2,10,"blue");
Cat cat2 = new Cat("小猫2",3,10,"red");
Cat cat3 = new Cat("小猫1",2,10,"blue");
System.out.println(cat1.equals(cat2));
System.out.println(cat1.equals(cat3));
}
}
Q2: java程序 一个主人有两只宠物,一条狗一只猫,狗叫旺财,猫叫小花,分别给宠物喂食
public class Person {
public static void main(String args[]) {
Animal a1 = new Animal("猫","小花","鱼");
Animal a2 = new Animal("狗","旺财"宠物java代码,"骨头");
a1.eat();a2.ert();
}
}
class Animal {
String species;
String animalName;
String foodName;
Animal(String species,String animalName,String foodName) {
this.species = species;
this.animalNam e= animalName;
this.foodName = foodName;
}
public void eat() {
System.out.print("我宠物java代码的"+species+","+animalName +" 吃宠物java代码了 :"+foodName);
}
}
//我也才学java不到一个月,我就能理解这么多
//还可以写继承的。亦可以写成多线程的。
//继承就是把Animal类当父类,再写两个类继承它。这里又可以复习多态,哈哈。
//多线程就是同事喂这俩动物。
Q3: JAVA大神来帮忙啊,ECLIPSE编写一个小程序类似于电子宠物的。弄完截图过来也行,行了就加大悬赏,谢谢!
其实是很简单的程序,就是无限循环和条件表达式的运用而已。代码如下:
import java.util.Scanner;
public class ElectronicDog {
private static double timeLeft = 0; // 剩余时间
private static int happiness = 1; // 幸福值(初始为1)
private static int energy = 2; // 能量值(初始为2)
private static Scanner sc;
private static double sleepTimeLeft = 10; // 睡眠时间间隔,超过则小狗离开
private static double feedTimeLeft = 0; // 饲养时间间隔,小于间隔则无效
public static void main(String[] args) {
sc = new Scanner(System.in);
int days = 0;
// 输入天数
while(days = 0) {
System.out.print("Play time (day(s)): ");
days = sc.nextInt();
if(days = 0) {
System.out.println("Please input a valid time.");
}
}
// 剩余时间就是天数乘以24小时
timeLeft = days * 24;
while(timeLeft 0) { // 如果剩余时间小于0则游戏结束
showState();
if(happiness = 0) { // 幸福值小于0时结束
System.out.println("The dog is out of happiness and has left.");
break;
}
if(energy = 0) { // 能量值小于0时结束
System.out.println("The dog is out of energy and has died.");
break;
}
if(sleepTimeLeft = 0) { // 睡眠时间超过时结束
System.out.println("The dog does not sleep within 10 hours and has left.");
break;
}
Command ops = getCommand();
if(ops == Command.Sleep) { // 如果是让宠物睡觉则睡眠时间重置
sleepTimeLeft = 10;
} else { // 如果不是让宠物睡觉则睡眠时间减去任务消耗时间
if(ops == Command.Feed) {
if(feedTimeLeft 0) { // 如果太过频繁喂养,则本次命令无效
continue;
} else { // 如果喂养有效,则喂养时间重置
feedTimeLeft = 3;
}
}
sleepTimeLeft -= ops.timeSpent;
}
feedTimeLeft -= ops.timeSpent;
happiness += ops.happinessGained;
energy += ops.energyConsumed;
timeLeft -= ops.timeSpent;
}
if(timeLeft = 0) { // 如果是正常游戏时间结束才能看到这一句
System.out.println("Play time is over.");
}
}
/**
* 显示宠物状态
*/
private static void showState() {
System.out.println("--------------------------------");
System.out.println("Time left: " + getTime());
System.out.println("Dog's happiness: " + happiness);
System.out.println("Dog's energy: " + energy);
}
/**
* 命令宠物行为
* @return 返回操作明细
*/
private static Command getCommand() {
System.out.println("1. Walk\r\n2. Feed\r\n3. Sleep\r\n4. Play");
int choice = 0;
while(choice = 0 || choice 4) {
System.out.print("Choice: ");
if(sc.hasNextInt()) {
choice = sc.nextInt();
}
}
Command cmd = Command.values()[choice - 1];
return cmd;
}
private static String getTime() {
int hour = (int) Math.floor(timeLeft);
double min = timeLeft - hour;
return String.format("%02d : %02d", hour, (int) (min * 60));
}
}
/**
* 对小狗发出的命令
*/
enum Command {
Walk(1, 3, -2), Feed(0.5f, 1, 5), Sleep(8, -8, -4), Play(2, 2, -1);
public float timeSpent;
public int happinessGained;
public int energyConsumed;
private Command(float timeSpent, int happinessGained, int energyConsumed) {
this.timeSpent = timeSpent;
this.happinessGained = happinessGained;
this.energyConsumed = energyConsumed;
}
}
部分截图如下
Q4: 不同宠物喂食java代码
public abstract Class Animal{
protected String color;
public void eat(){
}
public void shout(){
}
}
//派生类
public Class Dog extends Animal{
@Override
public void eat(){
System.out.println(color+"的狗在吃饭")
}
@Override
public void shout(){
System.out.println(color+"的狗在叫 ")
}
}
关于宠物java代码和java宠物类的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







