[英]Java unexpected type doing string comparison, strange
This code is exactly as it is in the book Learning Java (Oracle Press Books), but it doesn't work. 该代码与《学习Java》(Oracle Press图书)一书中的代码完全相同,但不起作用。 I don't understand why it doesn't work, it should work. 我不明白为什么它不起作用,它应该起作用。 I've tried it with both OpenJDK and Sun JDK 7, the error is the same. 我已经使用OpenJDK和Sun JDK 7尝试过,错误是相同的。
ThreadCom.java:56: error: unexpected type
if (thrd.getName().compareTo("Tick") = 0) {
^
required: variable
found: value
1 error
The code in question... 有问题的代码...
class MyThread implements Runnable {
Thread thrd;
TickTock ttOb;
MyThread(String name, TickTock tt) {
thrd = new Thread(this, name);
ttOb = tt;
thrd.start();
}
public void run() {
if (thrd.getName().compareTo("Tick") = 0) { // <- that line
for (int i=0; i<5; i++) ttOb.tick(true);
ttOb.tick(false);
} else {
for (int i=0; i<5; i++) ttOb.tock(true);
ttOb.tock(false);
}
}
}
The code is exactly as it is in the book. 代码与书中的代码完全相同。
this 这个
thrd.getName().compareTo("Tick") = 0
should be 应该
thrd.getName().compareTo("Tick") == 0
the first is an assignment. 首先是作业。 the second is a comparison. 第二个是比较。
I forgot the double equals, just noticed it. 我忘记了双重平等,只是注意到了。
if (thrd.getName().compareTo("Tick") = 0)
should be 应该
if (thrd.getName().compareTo("Tick") == 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.