下午!我在“电视”课上遇到了布尔方法的问题。所有我基本上想要完成的是无论何时调用该方法,我都希望切换powerStatus的值。
/**
* @author Thomas Samuel
* @version 1.0
* @since 22/02/2017
* <h1>Television Remote / Lab Sheet 2</h1>
* <p1>The following program is made for a television remote to control power, channel information, and volume.</p1>
*/
class Television {
Television Television = new Television();
boolean powerStatus = false;
int currentChannel;
int currentVolume = 50;
boolean togglePower(boolean powerStatus) {
if(powerStatus = false) {
powerStatus = true;
} else if(powerStatus = true) {
powerStatus = false;
}
return powerStatus;
}
boolean getPowerStatus(boolean powerStatus) {
return powerStatus;
}
}
public class Controller {
public static void main(String[] args) {
Television.togglePower();
}
}
我收到的错误如下:
Controller.java:27: error: method togglePower in class Television cannot be applied to given types;
Television.togglePower();
^
required: boolean
found: no arguments
reason: actual and formal argument lists differ in length
1 error
答案 0 :(得分:1)
您收到错误是因为您在没有参数的情况下调用togglePower
,但此方法需要boolean
参数。
将togglePower
方法更改为不参数,因为它已拥有powerStatus
变量:
boolean togglePower() {
powerStatus = !powerStatus;
return powerStatus;
}
以同样的方式,使用简单的getter方法获取参数是没有意义的,所以请尝试:
boolean getPowerStatus() {
return powerStatus;
}
答案 1 :(得分:0)
请仔细研究此代码以了解需要更改的内容
class Television {
boolean powerStatus = false;
int currentChannel;
int currentVolume = 50;
boolean togglePower() {
powerStatus = !powerStatus;
return powerStatus;
}
boolean getPowerStatus() {
return powerStatus;
}
}
public class Controller {
public static void main(String[] args) {
Television tv = new Television();
tv.togglePower();
System.out.println(tv.getPowerStatus());
}
}
我将实例化移动到main方法中,并将togglePower
更改为使用逻辑“not”来切换布尔值。