如何在决策树中添加或实现图像



我想知道你是否可以在决策中添加一个图像,这样当用户进入最后一个问题时,它会显示一个与用户输入的相匹配的图像

    <import java.util.Scanner;    
/**
 * Decision Tree implemented for people choosing a car brand based on specific qualifications
 * or characteristics.
 * @author Bart
 *
 */
public class CarBrandExpert {
     private LinkedBinaryTree<String> tree;
       //-----------------------------------------------------------------
       //  Sets up the diagnosis question tree.
       //-----------------------------------------------------------------
       public CarBrandExpert()
       {
          String e1 = "Do you like american cars?";
          String e2 = "Do you like fast cars? ";
          String e3 = "Do you like sports cars?";
          String e4 = "Do you like car durability? ";
          String e5 = "Do you like sleek designs?";
          String e6 = "Do you like off road driving?";
          String e7 = "You like heavy duty?";
          String e8 = "Do you like easy car handles while driving?";
          String e9 = "Does size matter??";
          String e10 = "Do nice interiors catch your eye?";
          String e11 = "Love to drive in style?";
          String e12 = "Does gas milage matter? ";
          String e13 = "Do you have an expensive taste?";
          String e14 = "Toyota is your brand! ";
          String e15 = " Nissan is your Brand! ";
          String e16 = "An Audi is what you need!";
          String e17 = "BMW suits you best!";
          String e18 = "Fords are for your basic needs based upon your data";
          String e19 = "Chevy is the BOMB! Definitely worth a try!";
          String e20 = "Try Chrysler";
          String e21 = "Dodges should quinch your thrist and relieve your worries";
//Creates the nodes in order to match the outcomes with the right questions as well predict the different outcome of users answer
          LinkedBinaryTree<String> n2, n3, n4, n5, n6, n7, n8, n9,
             n10, n11, n12, n13, n14, n15, n16, n17, n18, n19, n20, n21;

          n14 = new LinkedBinaryTree<String>(e14);
          n15 = new LinkedBinaryTree<String>(e15);
          n12 = new LinkedBinaryTree<String>(e12, n14, n15);

          n16 = new LinkedBinaryTree<String>(e16);
          n17 = new LinkedBinaryTree<String>(e17);
          n13 = new LinkedBinaryTree<String>(e13, n16, n17);
          n18 = new LinkedBinaryTree<String>(e18);
          n19 = new LinkedBinaryTree<String>(e19);
          n11 = new LinkedBinaryTree<String>(e11, n18, n19);
          n20 = new LinkedBinaryTree<String>(e20);
          n21 = new LinkedBinaryTree<String>(e21);
          n7 = new LinkedBinaryTree<String>(e7, n20, n21);
          n2 = new LinkedBinaryTree<String>(e2, n12, n13);
          n3 = new LinkedBinaryTree<String>(e3, n11, n7);

          tree = new LinkedBinaryTree<String>(e1, n2, n3);

       }
         /*
          * Prints out the decision based on the answer of yes or no
          * Also helps to check which side of the tree the program should go on in order to find predicted answer
          */
       public void Matchup()
          {
             Scanner scan = new Scanner(System.in);
             BinaryTree<String> current = tree;
             System.out.println ("So,you're looking to find the right car brand?");
             while (current.size() > 1)
             {
                System.out.println (current.getRootElement());
                if (scan.nextLine().equalsIgnoreCase("N"))
                   current = current.getLeft();
                else 
                   current = current.getRight();
             }
             System.out.println (current.getRootElement());
          }    



}>
    <public class CarBrandMatchUp {
    public static void main(String[] args) {
        CarBrandExpert car = new CarBrandExpert();
        car.Matchup();
    }
}>

我在想,对于每一个结果,用户都会与一个特定的汽车品牌相匹配,它会随机显示该品牌的汽车。我有什么想法可以做到这一点吗?

如果你想知道如何显示图像,我得到了以下代码java的文档(可以在这里找到):

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
/**
 * This class demonstrates how to load an Image from an external file
 */
public class LoadImageApp extends Component {
    BufferedImage img;
    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }
    public LoadImageApp() {
       try {
           img = ImageIO.read(new File("strawberry.jpg"));
       } catch (IOException e) {
       }
    }
    public Dimension getPreferredSize() {
        if (img == null) {
             return new Dimension(100,100);
        } else {
           return new Dimension(img.getWidth(null), img.getHeight(null));
       }
    }
    public static void main(String[] args) {
        JFrame f = new JFrame("Load Image Sample");
        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        f.add(new LoadImageApp());
        f.pack();
        f.setVisible(true);
    }
}

您可以创建一个具有图像名称的字符串变量,并根据用户在树中的哪个节点进行更改。希望这至少能帮助你朝着正确的方向前进。

最新更新