java - 为什么 inc/dec 通道不做任何事情?
问题描述
在下面的代码中,我的测试表明这些方法tvObject.incChannel(...)
并tvObject.decChannel(...)
没有改变我的代码中的任何内容。为什么?
package lab2;
public class TV {
private int channelNumber;
private int volumeLevel;
private String status = "ON";
public TV(int channel, int vol, String stat) {
status = stat;
setChannelNumber(channel);
setVolumeLevel(vol);
}
public void setChannelNumber(int channel) {
if (status.equalsIgnoreCase("ON")) {
channelNumber = channel;
} else if (status.equalsIgnoreCase("OFF")) {
System.out.println("Error TV is off");
}
}
public int getChannelNumber() {
return channelNumber;
}
public void setVolumeLevel(int vol) {
if (status.equalsIgnoreCase("ON")) {
volumeLevel = ((vol >= 0 && vol <= 100)?vol:0);
} else if (status.equalsIgnoreCase("OFF")) {
System.out.println("Error TV is off");
}
volumeLevel = ((vol >= 0 && vol <= 100)?vol:0);
}
public int getVolumeLevel() {
return volumeLevel;
}
public void incVol(int vol) {
if (status.equalsIgnoreCase("ON")) {
volumeLevel = ((vol >= 0 && vol <= 98)?vol += 2:volumeLevel);
} else if (status.equalsIgnoreCase("OFF")) {
System.out.println("Error TV is off");
}
}
public void decVol(int vol) {
if (status.equalsIgnoreCase("ON")) {
volumeLevel = ((vol >= 2 && vol <= 100)?vol -= 2:volumeLevel);
} else if (status.equalsIgnoreCase("OFF")) {
System.out.println("Error TV is off");
}
}
// does not do anything
public void incChannel(int channel) {
if (status.equalsIgnoreCase("ON")) {
channelNumber = channel++;
} else if (status.equalsIgnoreCase("OFF")) {
System.out.println("Error TV is off");
}
}
// does not do anything
public void decChannel(int channel) {
if (status.equalsIgnoreCase("ON")) {
channelNumber = channel--;
} else if (status.equalsIgnoreCase("OFF")) {
System.out.println("Error TV is off");
}
}
public void Toggle() {
//String status = "ON"; //default on
if (status.equalsIgnoreCase("ON")) {
status = "OFF";
} else if (status.equalsIgnoreCase("OFF")) {
status = "ON";
}
}
}
// code to test
package lab2;
public class Test {
public static void main(String[] args) {
TV tvObject = new TV(53, 35, "ON");
//TV tvObject2 = new TV(3, -10, "on");
//TV tvObject3 = new TV(3, -10, "OFF");
tvObject.Toggle();
tvObject.setChannelNumber(53);
tvObject.incChannel(53);
tvObject.setVolumeLevel(35);
tvObject.decVol(35);
System.out.println(tvObject.getChannelNumber());
System.out.println(tvObject.getVolumeLevel());
}
}
解决方案
它应该是这样的:
public void incChannel(int channel) {
if (status.equalsIgnoreCase("ON")) {
channelNumber = ++channel; // use either this
// channelNumber = channel+1; // or this
} else if (status.equalsIgnoreCase("OFF")) {
System.out.println("Error TV is off");
}
}
public void decChannel(int channel) {
if (status.equalsIgnoreCase("ON")) {
channelNumber = --channel; // use either this
// channelNumber = channel-1; // or this
} else if (status.equalsIgnoreCase("OFF")) {
System.out.println("Error TV is off");
}
}
channelNumber = channel++
表示先将通道分配给通道编号,然后再增加其值。
channelNumber = ++channel
表示先增加通道值,然后将其分配给 cannelNumber。
推荐阅读
- mysql - 是否可以使用 mysql 脚本获取列表对象的数据?
- node.js - jest-dev-server 的使用示例中的 config/start.js 是什么
- mysql - MySQL 连接/子查询,其中结果由外部表的多个值过滤
- php - 如何在不知道扩展名的情况下获取文件扩展名
- fibonacci - 为什么敏捷中的故事有斐波那契数列作为估计?
- android - Play Store CALL_LOG 和 SMS 权限不在 APK 中,但仍显示在商店中
- javascript - Chrome 扩展获取请求返回 Promise
- javascript - 未捕获的 SyntaxError Three.js FontLoader
- javascript - 检查页面上的链接是否包含字符串
- python - 在 Apache Airflow 中保存运算符的结果