在下面的代码中,我的测试表明方法 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());
}
}
最佳答案
It should be like this:
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++
表示先将channel分配给channelNumber,然后再增加其值。
channelNumber =++channel
表示先增加channel值,然后赋值给cannelNumber。
关于java - 为什么 inc/dec channel 不执行任何操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60118074/