[英]Is there a way to deal with 'java: bad operand types for binary operator '!='' in this piece of code?
I have this code and it is coming up with the following error:我有这段代码,它出现了以下错误:
java: bad operand types for binary operator ':=' first type: Node
java:二元运算符“:=”的错误操作数类型第一类型:节点
second type: int第二种类型:int
It is suppose to have the head be 3 and the next Node have data 4. Then the while loop is suppose to print each data from each Node.假设 head 为 3,下一个 Node 有数据 4。然后 while 循环假设打印来自每个 Node 的每个数据。
class Node {
Node next = null;
int data;
public Node(int d) {
data = d;
}
Node appendToTail(int d) {
Node end = new Node(d);
Node n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
return end;
}
public static void main(String[] args) {
Node t;
Node obj = new Node(3);
t = obj.appendToTail(4);
while (t != 0)
{
System.out.println(t);
}
}
}
//// ////
Expected output:
3 4
Your current code, even if fixed, would result in an infinite loop, because once the while loop is entered, its condition does not change.您当前的代码,即使是固定的,也会导致无限循环,因为一旦进入 while 循环,它的条件就不会改变。
I think you want:我想你想要:
while (t != null)
but that doesn't solve the infinite loop problem.但这并不能解决无限循环问题。
To have code that behaves as you expect:要让代码的行为符合您的预期:
for (t = obj; t != null; t = t.next) {
System.out.println(t.data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.