给完整的题目,图片看不太清楚。
追问这个就是完整题目了
追答public class TV {
private int channel = 1;
private int volumeLevel = 1;
private boolean on = false;
public TV() {
}
public void turnOn() {
this.on = true;
}
public void turnOff() {
this.on = false;
}
public void setChannel(int newChannel) {
if (!this.on)
return;
if (newChannel 120) {
this.channel = 120;
} else {
this.channel = newChannel;
}
}
public int getChannel() {
return this.channel;
}
public void setVolume(int newVolume) {
if (!this.on)
return;
if (newVolume 7) {
this.volumeLevel = 7;
} else {
this.volumeLevel = newVolume;
}
}
public int getVolume() {
return this.volumeLevel;
}
public void channelUp() {
this.setChannel(this.channel + 1);
}
public void channelDown() {
this.setChannel(this.channel - 1);
}
public void volumeUp() {
this.setChannel(this.volumeLevel + 1);
}
public void volumeDown() {
this.setChannel(this.volumeLevel - 1);
}
}
追问好的谢谢,我回去试试看
追答public class TV {
private int channel = 1;
private int volumnLevel = 1;
private boolean on = false;
public TV() {
}
public void turnOn() {
this.on = true;
}
public void turnOff() {
this.on = false;
}
public void setChannel(int newChannel) {
if (!this.on)
return;
if (newChannel < 1) {
this.channel = 1;
} else if (newChannel > 120) {
this.channel = 120;
} else {
this.channel = newChannel;
}
}
public int getChannel() {
return this.channel;
}
public void setVolumn(int newVolumn) {
if (!this.on)
return;
if (newVolumn < 1) {
this.volumnLevel = 1;
} else if (newVolumn > 7) {
this.volumnLevel = 7;
} else {
this.volumnLevel = newVolumn;
}
}
public int getVolumn() {
return this.volumnLevel;
}
public void channelUp() {
this.setChannel(this.channel + 1);
}
public void channelDown() {
this.setChannel(this.channel - 1);
}
public void volumnUp() {
this.setChannel(this.volumnLevel + 1);
}
public void volumnDown() {
this.setChannel(this.volumnLevel - 1);
}
}import java.util.Scanner;
public class TVTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// (1)
TV tv = new TV();
// (2)
tv.turnOn();
System.out.print("输入音量:");
int volumn = scanner.nextInt();
tv.setVolumn(volumn);
System.out.print("输入频道:");
int channel = scanner.nextInt();
tv.setChannel(channel);
// (3)
System.out.println("频道:" + tv.getChannel() + ",音量:"
+ tv.getVolumn());
// (4)
tv.channelUp();
tv.volumnDown();
// (5)
System.out.println("频道:" + tv.getChannel() + ",音量:"
+ tv.getVolumn());
// (6)
tv.turnOff();
}
}