Java oop编程题目

2024-11-19 14:35:08
推荐回答(2个)
回答1:

import java.util.Scanner;//定义接口
public interface animals {
String monkey = "猴子";
String elephant = "大象";
String peach = "桃子";
String banana = "香蕉";
}
//这是猴子的类
class monkey implements animals{
public void monkeyeat(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要给猴子吃的食物");
String food1 = sc.next();
if(food1.equals(peach)){
System.out.println("你给猴子吃的食物是"+peach+",请选择要给它吃好的还是坏的");
String choose1 = sc.next();
if(choose1.equals("好的")){
System.out.println("猴子吃到了好的"+peach+",猴子笑哈哈");
}else if(choose1.equals("坏的")){
System.out.println("猴子吃到了坏的"+peach+",哭了起来");
}else{
System.out.println("您的输入有误,请输入好的或者坏的");
}
}
else if(food1.equals(banana)){
System.out.println("你给猴子吃的食物是"+banana+",请选择要给它吃好的还是坏的");
String choose2 = sc.next();
if(choose2.equals("好的")){
System.out.println("猴子吃到了好的"+banana+",猴子跳起来");
}else if(choose2.equals("坏的")){
System.out.println("猴子吃到了坏的"+banana+",扔掉"+banana);
}else{
System.out.println("您的输入有误,请输入好的或者坏的");
}
}else{
System.out.println("您输入的食物有误,这里只有"+peach+"和"+banana+"两种食物");
}
}
}
//这是大象的类
class elephant implements animals{
public void elephanteat(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要给大象吃的食物");
String food2 = sc.next();
if(food2.equals(peach)){
System.out.println("你给大象吃的食物是"+peach+",大象不吃桃子");
}else if(food2.equals(banana)){
System.out.println("你给大象吃的食物是"+banana+",请选择要给它吃好的还是坏的");
String choose3 = sc.next();
if(choose3.equals("好的")){
System.out.println("大象吃到了好的"+banana+",大象翘鼻子");
}else if(choose3.equals("坏的")){
System.out.println("大象吃到了坏的"+banana+",吐了出来");
}else{
System.out.println("您的输入有误,请输入好的或者坏的");
}
}else {
System.out.println("您输入的食物有误,这里只有"+peach+"和"+banana+"两种食物");
}
}
}
//测试类
class Test{
public static void main(String []args){
monkey one = new monkey();
one.monkeyeat();
elephant two = new elephant();
two.elephanteat();
}
}

回答2:

动物接口,里面有个吃的方法,传入一个食物对象
public interface Animals {
String Eat(Food food);
}
猴子

public class Monkey implements Animals {
@Override
public String Eat(Food food) {
String str = "";
if(food.getName().equals("香蕉")&&food.getQuality().equals("good")){
str = "猴子吃到好的香蕉跳起来";
}else if(food.getName().equals("香蕉")&&food.getQuality().equals("bad")){
str = "猴子吃到吃到坏的哭起来";
}else if(food.getName().equals("桃子")&&food.getQuality().equals("good")){
str = "猴子吃到好的桃子跳起来";
}else if(food.getName().equals("桃子")&&food.getQuality().equals("bad")){
str = "猴子吃到吃到坏的哭起来";
}
return str;
}
}
大象

public class Elephant implements Animals {
@Override
public String Eat(Food food) {
String str = "";
if(food.getName().equals("香蕉")&&food.getQuality().equals("good")){
str = "大象吃到好的香蕉跳起来";
}else if(food.getName().equals("香蕉")&&food.getQuality().equals("bad")){
str = "大象吃到吃到坏的哭起来";
}else if(food.getName().equals("桃子")&&food.getQuality().equals("good")){
str = "大象吃到好的桃子跳起来";
}else if(food.getName().equals("桃子")&&food.getQuality().equals("bad")){
str = "大象吃到吃到坏的哭起来";
}
return str;
}
}
食物

public class Food {
//食物名称
private String name;
//食物品质
private String quality;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
}