一个包含几种动物的小资料库,根据输入的动物属性要求可以输出对应的动物名称;如查询不到应当输出其他语句。利用Java语言实现

程序设计思路:先给每个小动物添加各自的属性,再让用户输入一定的属性,最后让程序输出符合用户要求的动物名称。

1.先根据每个动物的属性创造各自的动物属性类,使每个动物都是单独的一个对象(类),利用Map集合来添加动物属性,xxxProperities()方法就是为了在主方法中返回Map集合类型,以便与用户输入的Map属性集合进行比较。

1.1 创建一个蝙蝠类(Y代表该动物具有该属性,N代表该动物不具有该属性)

package com.tylg.animal;

import java.util.HashMap;

public class Bat {
	HashMap<String, String> hm = new HashMap<>();
	public HashMap batProperities() {
		hm.put("下蛋", "N");
		hm.put("飞行", "Y");
		hm.put("陆生", "Y");
		hm.put("水生", "N");
		hm.put("鳞片", "N");
		hm.put("尾巴", "N");
		hm.put("羽毛", "N");
		hm.put("腿", "Y");
		hm.put("牙齿", "Y");
		hm.put("冷血", "Y");
		return hm;
	}
}

1.2 创建一个小鸟类(Y代表该动物具有该属性,N代表该动物不具有该属性)

package com.tylg.animal;

import java.util.HashMap;

public class Bird {
	HashMap<String, String> hm = new HashMap<>();
	public HashMap birdProperities() {
		hm.put("下蛋", "Y");
		hm.put("飞行", "Y");
		hm.put("陆生", "Y");
		hm.put("水生", "N");
		hm.put("鳞片", "N");
		hm.put("尾巴", "Y");
		hm.put("羽毛", "Y");
		hm.put("腿", "Y");
		hm.put("牙齿", "N");
		hm.put("冷血", "N");
		return hm;
	}
}

1.3 创建一个鳄鱼类(Y代表该动物具有该属性,N代表该动物不具有该属性)

package com.tylg.animal;

import java.util.HashMap;

public class Crocodile {
	HashMap<String, String> hm = new HashMap<>();
	public HashMap crocodileProperities() {
		hm.put("下蛋", "Y");
		hm.put("飞行", "N");
		hm.put("陆生", "Y");
		hm.put("水生", "Y");
		hm.put("鳞片", "Y");
		hm.put("尾巴", "Y");
		hm.put("羽毛", "N");
		hm.put("腿", "Y");
		hm.put("牙齿", "Y");
		hm.put("冷血", "Y");
		return hm;
	}
}

1.4 创建一个普通鱼类(Y代表该动物具有该属性,N代表该动物不具有该属性)

package com.tylg.animal;

import java.util.HashMap;

public class Fish {
	HashMap<String, String> hm = new HashMap<>();
	public HashMap fishProperities() {
		hm.put("下蛋", "Y");
		hm.put("飞行", "N");
		hm.put("陆生", "N");
		hm.put("水生", "Y");
		hm.put("鳞片", "Y");
		hm.put("尾巴", "Y");
		hm.put("羽毛", "N");
		hm.put("腿", "N");
		hm.put("牙齿", "N");
		hm.put("冷血", "Y");
		return hm;
	}
}

1.5创建一个猴子类(Y代表该动物具有该属性,N代表该动物不具有该属性)

package com.tylg.animal;

import java.util.HashMap;

public class Monkey {
	HashMap<String, String> hm = new HashMap<>();
	public HashMap monkeyProperities() {
		hm.put("下蛋", "N");
		hm.put("飞行", "N");
		hm.put("陆生", "Y");
		hm.put("水生", "N");
		hm.put("鳞片", "N");
		hm.put("尾巴", "Y");
		hm.put("羽毛", "N");
		hm.put("腿", "Y");
		hm.put("牙齿", "Y");
		hm.put("冷血", "N");
		return hm;
	}
}

1.6创建一个须鲸类(Y代表该动物具有该属性,N代表该动物不具有该属性)

package com.tylg.animal;

import java.util.HashMap;
import java.util.Map;

public class Whale {
	HashMap<String, String> map = new HashMap<>();
	public HashMap whaleProperities() {
		map.put("下蛋", "Y");
		map.put("飞行", "N");
		map.put("陆生", "N");
		map.put("水生", "Y");
		map.put("鳞片", "N");
		map.put("尾巴", "Y");
		map.put("羽毛", "N");
		map.put("腿", "N");
		map.put("牙齿", "N");
		map.put("冷血", "N");
		return map;
	}
}

2.主方法入口

2.1 printIn()函数就是让用户输入某个动物的属性值,其中的judge()方法就是为了使用户输入一个正确的答复(在此为 N or Y),否则让用户反复输入直到正确为止。

2.2 为每个动物创造各自的动物对象,让后利用Map集合中equals()方法对集合中键与值进行比较,若找到了对应的对象就输出相应的动物名称;若没有该种属性的动物就打印出未找到(Not Found)相应语句。

2.3 该程序获取动物名称的办法是通过get.class获取到相应的字节码文件;再利用tostring()将其转化为字符串,也就是类名;最后通过substring()属性截取到字节码的最后几个字符串,这样就清除了其他的包名。

package com.tylg.animal;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		HashMap<String, String> map = new HashMap<>();
		map = printIn();                           // 获取玩家的输入选项 ,将其存入s0~s9变量;
		
		System.out.println("您输入的动物属性是:" + map);
		
		// 创造各类动物对象
		Whale wh = new Whale();
		Bat ba = new Bat();
		Bird bi = new Bird();
		Crocodile cr = new Crocodile();
		Monkey mo = new Monkey();
		Fish fi = new Fish();                            
		
		if(wh.whaleProperities().equals(map)) {
			System.out.println("☺ 亲!经由该资料库分析得出您要找的动物是:" 
		+ wh.getClass().toString().substring(22));
		}else if(map.equals(ba.batProperities())){
			System.out.println("☺ 亲!经由该资料库分析得出您要找的动物是:" 
		+ ba.getClass().toString().substring(22));
		}else if(map.equals(bi.birdProperities())) {
			System.out.println("☺ 亲!经由该资料库分析得出您要找的动物是:" 
		+ bi.getClass().toString().substring(22));
		}else if(map.equals(cr.crocodileProperities())) {
			System.out.println("☺ 亲!经由该资料库分析得出您要找的动物是:" 
		+ cr.getClass().toString().substring(22));
		}else if(map.equals(mo.monkeyProperities())) {
			System.out.println("☺ 亲!经由该资料库分析得出您要找的动物是:" 
		+ mo.getClass().toString().substring(22));
		}else if(map.equals(fi.fishProperities())) {
			System.out.println("☺ 亲!经由该资料库分析得出您要找的动物是:" 
		+ fi.getClass().toString().substring(22));
		}else {
			System.out.println(" ☹[抱歉] ,臣妾在该资料库查询不到您所输入特征的动物");
		}
		
	}


	private static HashMap<String, String> printIn() {
		System.out.println("请您按照提示输入:Y或者N");
		System.out.println();
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请问:该物种下蛋吗?");	
		String s0 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种会飞吗?");
		String s1 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种可以在陆地上生存吗?");
		String s2 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种可以在水中生存吗?");
		String s3 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种有鳞片吗?");
		String s4 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种有尾巴吗?");
		String s5 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种有羽毛吗?");
		String s6 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种有腿吗?");
		String s7 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种有牙齿吗?");
		String s8 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		System.out.println("请问:该物种是冷血动物吗?");
		String s9 = judge(sc.nextLine());
		System.out.println("--------------------------------");
		
		HashMap<String, String> map = new HashMap<>();
		map.put("下蛋", s0);
		map.put("飞行", s1);
		map.put("陆生", s2);
		map.put("水生", s3);
		map.put("鳞片", s4);
		map.put("尾巴", s5);
		map.put("羽毛", s6);
		map.put("腿", s7);
		map.put("牙齿", s8);
		map.put("冷血", s9);
		return map;
	}
	
	public static String judge(String s) {
		Scanner sc = new Scanner(System.in);
		while(!(s.equals("Y") || s.equals("N"))) {
			System.out.println("您输入的字母有误,请重新输入");
			String ss = sc.nextLine();
			s = ss;
		}
		return s;
	}
}

最后给出一个输出效果图
(1)输入的与资料库中的属性匹配时:

请您按照提示输入:Y或者N

请问:该物种下蛋吗?
N
--------------------------------
请问:该物种会飞吗?
N
--------------------------------
请问:该物种可以在陆地上生存吗?
N
--------------------------------
请问:该物种可以在水中生存吗?
Y
--------------------------------
请问:该物种有鳞片吗?
N
--------------------------------
请问:该物种有尾巴吗?
Y
--------------------------------
请问:该物种有羽毛吗?
N
--------------------------------
请问:该物种有腿吗?
N
--------------------------------
请问:该物种有牙齿吗?
N
--------------------------------
请问:该物种是冷血动物吗?
N
--------------------------------
您输入的动物属性是:{羽毛=N, 冷血=N, 水生=Y, 牙齿=N, 下蛋=N, 鳞片=N, 陆生=N, 飞行=N, 尾巴=Y, 腿=N}
☺ 亲!经由该资料库分析得出您要找的动物是:Whale

(2)输入的属性匹配不到资料库中属性时,以及当用户输入的非Y or N 时的情况。

请您按照提示输入:Y或者N

请问:该物种下蛋吗?
N
--------------------------------
请问:该物种会飞吗?
F
您输入的字母有误,请重新输入
G
您输入的字母有误,请重新输入
N
--------------------------------
请问:该物种可以在陆地上生存吗?
N
--------------------------------
请问:该物种可以在水中生存吗?
N
--------------------------------
请问:该物种有鳞片吗?
N
--------------------------------
请问:该物种有尾巴吗?
Y
--------------------------------
请问:该物种有羽毛吗?
Y
--------------------------------
请问:该物种有腿吗?
Y
--------------------------------
请问:该物种有牙齿吗?
Y
--------------------------------
请问:该物种是冷血动物吗?
Y
--------------------------------
您输入的动物属性是:{羽毛=Y, 冷血=Y, 水生=N, 牙齿=Y, 下蛋=N, 鳞片=N, 陆生=N, 飞行=N, 尾巴=Y, 腿=Y}
 ☹[抱歉] ,臣妾在该资料库查询不到您所输入特征的动物

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值