实验报告
定义一个电视机类,实现电视机的基本功能(换台,调整音量,开关),并测试其功能。
import java.util.Scanner;
public class S3_1 {
public static class TV{
int tvChannel;
int tvVolume;
int tvSwitch;
public void ChangetvChannel(int tvChannel){
this.tvChannel=tvChannel;
System.out.println("现在是"+tvChannel+"台");
}
public void AdjusttvVolume(int changetvvolume) {
if(changetvvolume==1){
this.tvVolume++;
System.out.println("现在的音量是"+tvVolume);
if(tvVolume>=5){
System.out.println("音量已经最大了!");
}
}
else if(changetvvolume==2){
this.tvVolume--;
System.out.println("现在的音量是"+tvVolume);
if(tvVolume<=0){
System.out.println("音量已经最小了!");
}
}
else{
System.out.println("error!");
}
}
public void settvSwitch(int tvSwitch){
this.tvSwitch=tvSwitch;
if(tvSwitch==1){
System.out.println("TV已打开");
}
else if(tvSwitch==0){
System.out.println("TV已关闭");
}else{
System.out.println("error!");
}
}
}
public static void main(String[] args) {
TV tv = new TV();
System.out.println("TV现处于关闭状态,请进行如下选择:");
System.out.println("1--打开TV");
System.out.println("0--关闭TV");
Scanner sc=new Scanner(System.in);
int select=sc.nextInt();
tv.settvSwitch(select);
if(select==0){
System.out.println("程序已退出!");
}else {
while (select!=0) {
System.out.println("TV现处于开启状态,请进行如下选择:");
System.out.println("1--换台");
System.out.println("2--调整音量");
System.out.println("0--关闭TV");
select=sc.nextInt();
switch (select) {
case 1:
System.out.println("请输入你想要换的台数:");
int tvChannel = sc.nextInt();
tv.ChangetvChannel(tvChannel);
break;
case 2:
System.out.println("1--增大音量");
System.out.println("2--减小音量");
int changetvvolume = sc.nextInt();
tv.AdjusttvVolume(changetvvolume);
break;
case 0:
tv.settvSwitch(select);
break;
default:
System.out.println("error!");
}
}
}
}
}