- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用二元决策树开发一项调查。用户会收到一个问题,他们回答"is"或“否”,答案决定接下来要问什么问题。
我的问题是代码只能在左侧工作,这意味着用户只能回答“否”,代码才能正常工作。如果用户给出的答案是"is",则会抛出 NullPointerException。难道是我做错了什么
import java.util.Scanner;
public class MusicGenreExpert {
private static LinkedBinaryTree<String> tree;
static Scanner scan = new Scanner(System.in);
public MusicGenreExpert() throws ElementNotFoundException
{
String e1 = "Are you eco-friendly?";
String e2 = "Do you consider yourself to be outgoing?";
String e3 = "Do you consider yourself to be an introvert?";
String e4 = "On an average day, do you think that you approach and solve" +
"problems differently than others?";
String e5 = "Are you often uneasy (nervous)?";
String e6 = "If the person in front of you dropped money without knowing," +
"would you pick it up and keep it?";
String e7 = "Is gentle a good word that describes you?";
String e8 = "Do you often worry about what other people may think?";
String e9 = "Do you think highly of yourself, with little to no care of" +
"what others may think?";
String e10 = "Are you happy with the accomplishments you've had in life" +
"thus far?";
String e11 = "Would you consider yourself to be old fashion?";
String e12 = "Do you put your all in everything that you do?";
String e13 = "Is your idea of an epic night going to a rave with thousands" +
"of people?";
String e14 = "Do you believe that Bob Marley is one of the greatest artist" +
"of all time?";
String e15 = "Would you call yourself an extravert?";
String e16 = "Do you know remember when you fell in love with" +
"Hip Hop?";
String e17 = "You are the music genre classical. You are a creative introvert"+
"with a good sense of self esteem. You are at ease with yourself as well as"+
"the world around you.";
String e18 = "The indie and punk music genre best fits you. You are an intellectual" +
"and creative introvert. You prefer not to work as hard as others and are less gentle." +
"Your self esteem is a constant battle.";
String e19 = "Rock/Heavy Metal best describes you. You are a creative and gentle" +
"introvert with a constant battle with self esteem. However you are no stranger to" +
"hard work.";
String e20 = "You are a high self esteem, honest and convetional" +
"introvert. The Pop culture is who you are inside. You may lack in creativity," +
"but you make up for in hard work.";
String e21 = " You remember the exact day you fell in love with Hip Hop." +
"\n With your high self esteem and outgoing personality, you are sure to be" +
"\n the life of the party. However, you could be more eco friendly and less" +
" aggressive.";
String e22 = "You are jazz, blues, and soul. You are a lot more extraverted" +
"than others. Your at ease personality, high self esteem, and creativity" +
"makes you the person that eveyone wants to know.";
String e23 = "The electronic/dance genre describes exactly who you are." +
"Assertive, outgoing, creative, yet aggressive gives everyone that comes" +
"in contact with you something to love. You aren't as kind as others, but" +
"hey, they love you anyway.";
String e24 = "Music is suppose to tell a story, and country music does" +
"just that. You are hardworking, conventional and outgoing. Oh and contrary" +
"to what others may believe your emotional stability is just fine.";
String e25 = "There is no other genre better to describe you than Reggae." +
"You are a creative outgoing individual, with high self esteem. Where you" +
"lack in hardwork you make up for in your kind and generous ways.";
LinkedBinaryTree<String> n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14,
n15, n16, n17, n18, n19, n20, n21, n22, n23, n24, n25, n26, n27, n28, n29, n30,
n31, n32, n33, n34, n35, n36, n37, n38, n39, n40, n41, n42, n43, n44, n45, n46,
n47, n48, n49, n50, n51, n52, n53;
n50 = new LinkedBinaryTree<String> (e16);
n51 = new LinkedBinaryTree<String> (e17);
n33 = new LinkedBinaryTree<String> (e8, n50, n51);
n52 = new LinkedBinaryTree<String> (e16);
n53 = new LinkedBinaryTree<String> (e18);
n49 = new LinkedBinaryTree<String> (e8, n52, n53);
n32 = new LinkedBinaryTree<String> (e21);
n16 = new LinkedBinaryTree<String> (e15, n32, n33);
n34 = new LinkedBinaryTree<String> (e17);
n35 = new LinkedBinaryTree<String> (e18);
n18 = new LinkedBinaryTree<String> (e7, n34, n35);
n36 = new LinkedBinaryTree<String> (e18);
n37 = new LinkedBinaryTree<String> (e19);
n19 = new LinkedBinaryTree<String> (e12, n36, n37);
n38 = new LinkedBinaryTree<String> (e16);
n39 = new LinkedBinaryTree<String> (e20);
n23 = new LinkedBinaryTree<String> (e16, n38, n39);
n40 = new LinkedBinaryTree<String> (e21);
n41 = new LinkedBinaryTree<String> (e23);
n25 = new LinkedBinaryTree<String> (e8, n40, n41);
n42 = new LinkedBinaryTree<String> (e22);
n43 = new LinkedBinaryTree<String> (e20);
n26 = new LinkedBinaryTree<String> (e16, n42, n43);
n44 = new LinkedBinaryTree<String> (e23);
n45 = new LinkedBinaryTree<String> (e19);
n27 = new LinkedBinaryTree<String> (e6, n44, n45);
n46 = new LinkedBinaryTree<String> (e17);
n47 = new LinkedBinaryTree<String> (e24);
n28 = new LinkedBinaryTree<String> (e14, n46, n47);
n48 = new LinkedBinaryTree<String> (e17);
n30 = new LinkedBinaryTree<String> (e7, n48, n49);
n17 = new LinkedBinaryTree<String> (e18);
n8 = new LinkedBinaryTree<String> (e7, n16, n17);
n9 = new LinkedBinaryTree<String> (e9, n18, n19);
n20 = new LinkedBinaryTree<String> (e17);
n21 = new LinkedBinaryTree<String> (e18);
n10 = new LinkedBinaryTree<String> (e7, n20, n21);
n22 = new LinkedBinaryTree<String> (e19);
n11 = new LinkedBinaryTree<String> (e5, n22, n23);
n24 = new LinkedBinaryTree<String> (e19);
n12 = new LinkedBinaryTree<String> (e10, n24, n25);
n13 = new LinkedBinaryTree<String> (e11, n26, n27);
n29 = new LinkedBinaryTree<String> (e19);
n14 = new LinkedBinaryTree<String> (e12, n28, n29);
n31 = new LinkedBinaryTree<String> (e22);
n15 = new LinkedBinaryTree<String> (e13, n30, n31);
n4 = new LinkedBinaryTree<String> (e6, n8, n9);
n5 = new LinkedBinaryTree<String> (e9, n10, n11);
n6 = new LinkedBinaryTree<String> (e4, n12, n13);
n7 = new LinkedBinaryTree<String> (e3, n14, n15);
n2 = new LinkedBinaryTree<String> (e2, n4, n5);
n3 = new LinkedBinaryTree<String> (e3, n6, n7);
tree = new LinkedBinaryTree<String> (e1, n2, n3);
}
public static void diagnose(){
BinaryTree<String> current = tree;
System.out.println(" What music genre are you? ");
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());
}
}
这是实现 BinaryTree 的 LinkedBinaryTree:
import java.util.Iterator;
public class LinkedBinaryTree<T> implements BinaryTree<T>
{
protected BTNode<T> root;
public LinkedBinaryTree(){
root = null;
}
public LinkedBinaryTree (T element)
{
root = new BTNode<T>(element);
}
public LinkedBinaryTree (T element, LinkedBinaryTree<T> left,
LinkedBinaryTree<T> right)
{
root = new BTNode<T>(element);
root.setLeft(left.root);
root.setRight(right.root);
}
public T getRootElement()
{
if (root == null)
throw new EmptyCollectionException ("Get root operation "
+ "failed. The tree is empty.");
return root.getElement();
}
public LinkedBinaryTree<T> getLeft()
{
if (root == null)
throw new EmptyCollectionException ("Get left operation "
+ "failed. The tree is empty.");
LinkedBinaryTree<T> result = new LinkedBinaryTree<T>();
result.root = root.getLeft();
return result;
}
public T find (T target)
{
BTNode<T> node = null;
if (root != null)
node = root.find(target);
if (node == null)
throw new ElementNotFoundException("Find operation failed. "
+ "No such element in tree.");
return node.getElement();
}
public int size()
{
int result = 0;
if (root != null)
result = root.count();
return result;
}
public Iterator<T> iterator()
{
return new ArrayIterator<T>();
}
public int height()
{
int result = 0;
if (root == null)
return result;
if (root != null)
result = root.height();
return result;
}
public BinaryTree<T> getRight() {
// TODO Auto-generated method stub
return null;
}
public boolean contains(T target) {
// TODO Auto-generated method stub
return false;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
}
最佳答案
现在您已经提供了 LinkedBinaryTree
的源代码,问题就清楚了。唯一一次调用 getRight()
是当您对其中一个决策回答 Y
或"is"时,该决策将执行这段代码:
public BinaryTree<T> getRight() {
// TODO Auto-generated method stub
return null;
}
由于它返回 null
,因此下次尝试取消引用 current
时(这发生在代码 while(current.size() > 1)
中),它会导致 NullPointerException
。
getRight()
方法需要类似于 getLeft()
实现。
关于java - 决策树执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171990/
是否有显示测试用例数量以提供决策/条件覆盖率的工具? 例如: if(x>0) if(x0) 3 个案例足以覆盖决策/条件。 if(x>0) if(x0) 4 个案例足以覆盖决策/条件。 这是真的吗?
我正在尝试找到一种优雅的方式来实现易于维护的决策算法,因为决策的条件可能经常变化。 我将尝试更具体地举一个例子: 假设我正在尝试管理一家餐厅厨房的 cooking 厨师团队。 每个厨师都知道如何 co
我需要一个 Android Activity ,它应该显示一个字段,如带有图像的标题和其下方的几个动态生成的项目(我认为是 1 到 100)。如果我不想让 headsection 滚动,我会使用 Li
我正在编写函数以从值列表中提供最大值(value)。我的问题是如果所有值都相同怎么办?例如, 30,29,34,45 简单。最大值为 45。现在, 20,20,20,20 这里的最大值是20吗?或者没
我需要知道哪个检索事件日志的速度更快,但我在比较中找不到:假设需要查找的所有列都有btree索引,需要查找的json对象中的所有键都有GIN索引。 case 1: ActivityID (in
我需要在我的 iPhone 应用程序中显示一个表格: neither the number of cells nor the contents are known at compile time, b
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
这是针对在 MinGW/Windows 上使用 SDL 的小型游戏项目。 我正在研究一个物理引擎,我的想法是拥有一个Physics::Object,所有物理对象都应该派生自它,并且它会在全局 Phys
我有一个小的 LINQ 查询来填充下拉控件(WinForms Telerik 应用程序),其中的数据行显示两个值(ITNBR 和描述): var query = from i in db.ItemMa
我正在尝试使用 antlr 3 为我的语法生成词法分析器和解析器。有人可以解释这个错误是什么意思吗? error(211): T.g:14:6: [fatal] rule stmt has non-L
partykit包很好地表示了决策树。我遇到的唯一问题是标签很长然后它们重叠。是否可以移动这些标签以防止它(见下图中的蓝色箭头)? library("rpart") library("partykit
所以我环顾四周,似乎找不到合适的解决方案来解决我的问题。 问题 在我的布局中,我希望能够根据数据库中的内容选择在运行时是否存在导航项: 当前布局(导航栏) @Html.Acti
我目前正在创建一个机器学习 jupyter 笔记本作为一个小项目,并希望显示我的决策树。但是,我能找到的所有选项都是导出图形然后加载图片,这相当复杂。 所以想问问有没有办法不用导出加载图形,直接显示我
grammar AdifyMapReducePredicate; PREDICATE : PREDICATE_BRANCH | EXPRESSION ; PREDICA
我是一名优秀的程序员,十分优秀!