自定义数据结构,方法不限
Rule1:IF该动物有毛发THEN该动物是哺乳动物
Rule2:IF该动物有奶THEN该动物是哺乳动物
Rule3:IF该动物有羽毛THEN该动物是鸟
Rule4:IF该动物会飞AND会下蛋THEN该动物是鸟
Rule5:IF该动物吃肉THEN该动物是肉食动物
Rule6:IF该动物有犬齿AND有爪AND眼盯前方THEN该动物是肉食动物
Rule7:IF该动物是哺乳动物AND有蹄THEN该动物是有蹄类动物
Rule8:IF该动物是哺乳动物AND是嚼反刍动物THEN该动物是有蹄类动物
Rule9:IF该动物是哺乳动物AND是肉食动物AND是黄褐色AND身上有暗斑点THEN该动物是金钱豹
Rule10:IF该动物是哺乳动物AND是肉食动物AND是黄褐色AND身上有黑色条纹THEN该动物是老虎
Rule11:IF该动物是有蹄类动物AND有长脖子AND有长腿AND身上有暗斑点THEN该动物是有长颈鹿
Rule12:IF该动物是有蹄类动物AND身上有黑色条纹THEN该动物是斑马
Rule13:IF该动物是鸟AND有长脖子AND有长腿AND不会飞THEN该动物是鸵鸟
Rule14:IF该动物是鸟AND会游泳AND有黑白二色AND不会飞THEN该动物是企鹅
Rule15:IF该动物是鸟AND善飞THEN该动物是信天翁
本次实验的简单动物识别系统总共能识别7种动物,即:老虎、金钱豹、斑马、长颈鹿、企鹅、鸵鸟、信天翁。
package 人工智能导论1;import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;public class Menu {JFrame frame=new JFrame("识别动物");JLabel label0=new JLabel("请选择以下条件");JLabel label1=new JLabel("1",JLabel.CENTER);JLabel label2=new JLabel("2",JLabel.CENTER);JLabel label3=new JLabel("3",JLabel.CENTER);JLabel label4=new JLabel("4",JLabel.CENTER);JLabel label5=new JLabel("5",JLabel.CENTER);JLabel label6=new JLabel("6",JLabel.CENTER);JLabel label7=new JLabel("7",JLabel.CENTER);JLabel label8=new JLabel("8",JLabel.CENTER);JLabel label9=new JLabel("9",JLabel.CENTER);JLabel label10=new JLabel("10",JLabel.CENTER);JLabel label11=new JLabel("11",JLabel.CENTER);JLabel label12=new JLabel("12",JLabel.CENTER);JLabel label13=new JLabel("13",JLabel.CENTER);JLabel label14=new JLabel("14",JLabel.CENTER);JLabel label15=new JLabel("15",JLabel.CENTER);JLabel label16=new JLabel("16",JLabel.CENTER);JLabel label17=new JLabel("17",JLabel.CENTER);JLabel label18=new JLabel("18",JLabel.CENTER);JLabel label19=new JLabel("19",JLabel.CENTER);JLabel label20=new JLabel("20",JLabel.CENTER);JCheckBox label1_=new JCheckBox("有毛发");JCheckBox label2_=new JCheckBox("有奶");JCheckBox label3_=new JCheckBox("有羽毛");JCheckBox label4_=new JCheckBox("会飞");JCheckBox label5_=new JCheckBox("会下蛋");JCheckBox label6_=new JCheckBox("吃肉");JCheckBox label7_=new JCheckBox("有犬齿");JCheckBox label8_=new JCheckBox("有爪");JCheckBox label9_=new JCheckBox("眼盯前方");JCheckBox label10_=new JCheckBox("有蹄");JCheckBox label11_=new JCheckBox("嚼反刍动物");JCheckBox label12_=new JCheckBox("长腿");JCheckBox label13_=new JCheckBox("长脖子");JCheckBox label14_=new JCheckBox("黑条纹");JCheckBox label15_=new JCheckBox("有暗斑点");JCheckBox label16_=new JCheckBox("不会飞");JCheckBox label17_=new JCheckBox("会游泳");JCheckBox label18_=new JCheckBox("有黑白二色");JCheckBox label19_=new JCheckBox("善飞");JCheckBox label20_=new JCheckBox("黄褐色");JPanel p1=new JPanel();JPanel p2=new JPanel();JPanel p3=new JPanel();JPanel p4=new JPanel();JPanel p5=new JPanel();JPanel p6=new JPanel();JPanel p7=new JPanel();JPanel p8=new JPanel();
JButton but1=new JButton("提交");JButton but2=new JButton("重置");public Menu() {frame.setSize(700,400);p1.add(label0);p2.add(label1); p2.add(label1_);p2.add(label2); p2.add(label2_);p2.add(label3); p2.add(label3_);p2.add(label4); p2.add(label4_);p3.add(label5); p3.add(label5_);p3.add(label6); p3.add(label6_);p3.add(label7);p3.add(label7_);p3.add(label8);p3.add(label8_);p4.add(label9); p4.add(label9_);p4.add(label10); p4.add(label10_);p4.add(label11);p4.add(label11_);p4.add(label12);p4.add(label12_);p5.add(label13); p5.add(label13_);p5.add(label14); p5.add(label14_);p5.add(label15); p5.add(label15_);p5.add(label16); p5.add(label16_);p6.add(label17); p6.add(label17_);p6.add(label18);p6.add(label18_);p6.add(label19); p6.add(label19_);p6.add(label20); p6.add(label20_);p8.add(but1);p8.add(but2);p7.add(p1);p7.add(p2);p7.add(p3);p7.add(p4);p7.add(p5);p7.add(p6);p7.add(p8);frame.add(p7);p1.setLayout(new GridLayout(1,1));
p2.setLayout(new GridLayout(1,8));p3.setLayout(new GridLayout(1,8));p4.setLayout(new GridLayout(1,8));p5.setLayout(new GridLayout(1,8));p6.setLayout(new GridLayout(1,8));p7.setLayout(new GridLayout(7,8,10,10));frame.setLocationRelativeTo(null);frame.setVisible(true);but1.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {if(label1_.isSelected()&&label2_.isSelected()&&label6_.isSelected()&&label7_.isSelected()&&label8_.isSelected()&&label9_.isSelected()&&label15_.isSelected()&&label20_.isSelected()&&!label3_.isSelected()&&!label11_.isSelected()&&!label16_.isSelected()&&!label4_.isSelected()&&!label12_.isSelected()&&!label17_.isSelected()&&!label5_.isSelected()&&!label12_.isSelected()&&!label18_.isSelected()&&!label10_.isSelected()&&!label14_.isSelected()&&!label19_.isSelected()) {JLabel ll=new JLabel("该动物是金钱豹");JOptionPane.showMessageDialog(null, ll);}if(label1_.isSelected()&&label2_.isSelected()&&label6_.isSelected()&&label7_.isSelected()&&label8_.isSelected()&&label9_.isSelected()&&label14_.isSelected()&&label20_.isSelected()&&!label3_.isSelected()&&!label11_.isSelected()&&!label16_.isSelected()&&!label4_.isSelected()&&!label12_.isSelected()&&!label17_.isSelected()&&!label5_.isSelected()&&!label12_.isSelected()&&!label18_.isSelected()&&!label10_.isSelected()&&!label15_.isSelected()&&!label19_.isSelected()) {JLabel ll=new JLabel("该动物是老虎");JOptionPane.showMessageDialog(null, ll);}if(label1_.isSelected()&&label2_.isSelected()&&label10_.isSelected()&&label11_.isSelected()&&label13_.isSelected()&&label12_.isSelected()&&label15_.isSelected()&&!label3_.isSelected()&&!label4_.isSelected()&&!label8_.isSelected()&&!label16_.isSelected()&&!label5_.isSelected()&&!label9_.isSelected()&&!label17_.isSelected()&&!label6_.isSelected()&&!label20_.isSelected()&&!label18_.isSelected()&&!label7_.isSelected()&&!label14_.isSelected()&&!label19_.isSelected()) {JLabel ll=new JLabel("该动物是长颈鹿");JOptionPane.showMessageDialog(null, ll);}if(label1_.isSelected()&&label2_.isSelected()&&label10_.isSelected()&&label11_.isSelected()&&label14_.isSelected()&&!label13_.isSelected()&&!label12_.isSelected()&&!label4_.isSelected()&&!label8_.isSelected()&&!label16_.isSelected()&&!label5_.isSelected()&&!label9_.isSelected()&&!label17_.isSelected()&&!label6_.isSelected()&&!label13_.isSelected()&&!label18_.isSelected()&&!label7_.isSelected()&&!label15_.isSelected()&&!label19_.isSelected()&&!label20_.isSelected()) {JLabel ll=new JLabel("该动物是斑马");JOptionPane.showMessageDialog(null, ll);}
if(label3_.isSelected()&&label4_.isSelected()&&label5_.isSelected()&&label13_.isSelected()&&label12_.isSelected()&&label16_.isSelected()&&!label1_.isSelected()&&!label2_.isSelected()&&!label10_.isSelected()&&!label17_.isSelected()&&!label6_.isSelected()&&!label11_.isSelected()&&!label18_.isSelected()&&!label7_.isSelected()&&!label14_.isSelected()&&!label19_.isSelected()&&!label8_.isSelected()&&!label15_.isSelected()&&!label20_.isSelected()&&!label9_.isSelected()) {JLabel ll=new JLabel("该动物是鸵鸟");JOptionPane.showMessageDialog(null, ll);}if(label3_.isSelected()&&label4_.isSelected()&&label5_.isSelected()&&label17_.isSelected()&&label18_.isSelected()&&label16_.isSelected()&&!label1_.isSelected()&&!label2_.isSelected()&&!label10_.isSelected()&&!label13_.isSelected()&&!label6_.isSelected()&&!label11_.isSelected()&&!label12_.isSelected()&&!label7_.isSelected()&&!label14_.isSelected()&&!label19_.isSelected()&&!label8_.isSelected()&&!label15_.isSelected()&&!label20_.isSelected()&&!label9_.isSelected()) {JLabel ll=new JLabel("该动物是企鹅");JOptionPane.showMessageDialog(null, ll);}if(label3_.isSelected()&&label4_.isSelected()&&label5_.isSelected()&&label19_.isSelected()&&!label12_.isSelected()&&!label16_.isSelected()&&!label1_.isSelected()&&!label2_.isSelected()&&!label10_.isSelected()&&!label17_.isSelected()&&!label6_.isSelected()&&!label11_.isSelected()&&!label18_.isSelected()&&!label7_.isSelected()&&!label14_.isSelected()&&!label17_.isSelected()&&!label8_.isSelected()&&!label15_.isSelected()&&!label20_.isSelected()&&!label9_.isSelected()) {JLabel ll=new JLabel("该动物是信天翁");JOptionPane.showMessageDialog(null, ll);}
else {JLabel ll=new JLabel("该动物不存在");JOptionPane.showMessageDialog(null, ll);}}});but2.addActionListener(new ActionListener(){@Overridepublic void actionPerformed(ActionEvent e) {label1_.setSelected(false);label8_.setSelected(false);label15_.setSelected(false);label2_.setSelected(false);label9_.setSelected(false);label16_.setSelected(false);label3_.setSelected(false);label10_.setSelected(false);label17_.setSelected(false);label4_.setSelected(false);label11_.setSelected(false);label18_.setSelected(false);label5_.setSelected(false);label12_.setSelected(false);label19_.setSelected(false);label6_.setSelected(false);label13_.setSelected(false);label20_.setSelected(false);label7_.setSelected(false);label14_.setSelected(false);}});}public static void main(String[] args) {new Menu();}
}