gpt4 book ai didi

Java:无法解析为类型

转载 作者:行者123 更新时间:2023-11-29 07:53:36 24 4
gpt4 key购买 nike

下面我定义嵌套类的代码有什么问题?它提示说:CNode cannot be resolved to a type

package compute;

public class CBTree {

public CNode root;

public void addNode(int key){
CNode newNode = new CNode(key);
// now to find an appropriate place for this new node

// 1 it could be placed at root
if (null == root){ // to void c-style mistakes a==null ( with a=null) is not prefered
root = newNode;
}
// if not then find the best spot ( which will be once of the
CNode currentRoot = root;
while(true){
if (key < currentRoot.key){
if (null == currentRoot.left){
currentRoot.left = newNode;
break;
} else{
currentRoot = currentRoot.left;
} else{//if (key < currentRoot.key)
if (null == currentRoot.right){
currentRoot.right = newNode;
break;
}else{
currentRoot = currentRoot.right;
}

}

}//while



class CNode{
int key;
public CNode left;
public CNode right;
/**
* Constructor
*/
public CNode(int key){
this.key = key;
}
/**
* Display the node
*/
public void display(){
System.out.println("node:"+ key);
}

}


}

最佳答案

CNode 类在addNode 方法中定义。

将您的 CNode 类放在 addNode 方法之外,以便它可以解析。

此外,您需要调整您的 if/else 逻辑,因为您目前在同一个 if 上有两个 else block ,它们将无法编译。

关于Java:无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19433917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com