当前位置:   article > 正文

java实验报告——电视机类_java电视机类

java电视机类

实验报告

定义一个电视机类,实现电视机的基本功能(换台,调整音量,开关),并测试其功能。

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!");
                    }
                }
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/475519
推荐阅读
相关标签
  

闽ICP备14008679号