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

Java 如何在决策树中添加或实现图像,java,Java,我想知道您是否可以在决策中添加一个图像,这样当用户进入最后一个问题时,它会显示一个与用户输入的内容相匹配的图像 <import java.util.Scanner; /** * Decision Tree implemented for people choosing a car brand based on specific qualifications * or characteristics. * @author Bart * */ public class

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

    <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();

    }

}>
1)
{
System.out.println(current.getRootElement());
if(scan.nextLine().equalsIgnoreCase(“N”))
current=current.getLeft();
其他的
current=current.getRight();
}
System.out.println(current.getRootElement());
}    
}>

我在想,对于每个结果,用户将与特定的汽车品牌相匹配,它将随机显示该品牌的汽车。我如何做到这一点呢?

如果您想知道如何显示图像,我得到了以下代码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);
    }
}