JAVA编程题

如题所述

第1个回答  2018-03-15

运行结果:

代码如下:

class TV {

private int channel;

private int volumeLevel;

private boolean on;

public TV() {
this.channel = 1;
this.volumeLevel = 1;
this.on = false;
}

public void turnOn() {
this.on = true;
}

public void turnOff() {
this.on = false;
}

public int getChannel() {
return channel;
}

public void setChannel(int channel) {
if (channel < 1 || channel > 120) {
throw new IllegalArgumentException("无效的频道。");
}
this.channel = channel;
}

public int getVolumn() {
return volumeLevel;
}

public void setVolumn(int newVolumn) {
if (newVolumn < 1 || newVolumn > 7) {
throw new IllegalArgumentException("无效的音量。");
}
this.volumeLevel = newVolumn;
}

public void channelUp() {
if (this.channel < 120) {
this.channel++;
}
}

public void channelDown() {
if (this.channel > 1) {
this.channel--;
}
}

public void volumeUp() {
if (this.volumeLevel < 7) {
this.volumeLevel++;
}
}

public void volumeDown() {
if (this.volumeLevel > 1) {
this.volumeLevel--;
}
}
}

public class TVTest {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// 创建 TV 对象
TV tv = new TV();

// 打开 tv 
tv.turnOn();

System.out.print("请输入音量:");
int volumn = scanner.nextInt();
tv.setVolumn(volumn);


System.out.print("请输入频道:");
int channel = scanner.nextInt();
tv.setChannel(channel);

System.out.println("当前频道:" + tv.getChannel() + ",当前音量:" + tv.getVolumn());

// 频道加1
tv.channelUp();

// 音量减1
tv.volumeDown();

System.out.println("当前频道:" + tv.getChannel() + ",当前音量:" + tv.getVolumn());

// 关闭 tv
tv.turnOff();
}
}

本回答被提问者和网友采纳
第2个回答  2018-03-15
给完整的题目,图片看不太清楚。追问

这个就是完整题目了

追答

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();
}

}

第3个回答  2018-03-15
留个邮箱 我发给你追问

谢谢啦

第4个回答  2018-03-15


拿好

追答

图片好像被百度知道压缩了,没法看了。