电视开启或关闭Java代码

问题描述 投票:0回答:3

我在eclipse在线学习中有一个Java类的项目,我在理解某些东西时遇到了问题。在我的书中,对这种情况没有任何解释,我的老师也没用。

我的项目是创建一个对象类电视,并从开启到关闭更改电源,只有当电源打开时,我必须更改通道5次和音量一次。

我知道我必须创建一个布尔方法if(power==true)但我不知道如何做到这一点以及如何将它与我的代码结合起来。

这是我的代码:

public class TelevisionDemo {

    public static void main(String[] args) {
        //create television object
        Television tv = new Television ();

        //invoke call methods on the object tv
        tv.changeChannel (1);
        tv.changeVolume (8);
        tv.printStatus ();

        System.out.println("I will change the the volume one time and the channel 5 times");
        tv.changeChannel(2);    tv.changeVolume(6);     tv.printStatus();
        tv.changeChannel(3);        tv.printStatus();
        tv.changeChannel(4);        tv.printStatus();
        tv.changeChannel(8);        tv.printStatus();
        tv.changeChannel(5);        tv.printStatus();

        }

}

//this is the blueprint
class Television {

    boolean power = true; //create a method for tv powerOnOff 
    int channel = 0;
    int volume = 0;


    void changeChannel (int newValue){//method to change the channel 
            channel = newValue; 
    }

    void changeVolume (int newValue){ //method to change the volume 
                volume = newValue;
    }

    void printStatus(){ //printing the status of the Television, channel and volume                 
        System.out.println("Channel: " + channel + " Volume: " + volume);
    }
}

我已经创建了一个方法powerOnpowerOff并在main方法中调用/调用它但我仍然不知道如何在电视电源打开时让我只更改频道和音量的所有参数。

任何人都可以帮我解决这个问题吗?

这是我的代码:

public class TelevisionDemo {

public static void main(String[] args) {
    Television tv = new Television ();//create television object

    //invoke call methods on the object tv
    tv.powerOn();
    tv.powerOff();
    tv.changeChannel (1);
    tv.changeVolume (2);
    tv.printStatus ();

    System.out.println("I will change the the volume one time and the channel 5 times");
    tv.changeChannel(2);    tv.changeVolume(6);     tv.printStatus();
    tv.changeChannel(3);        tv.printStatus();
    tv.changeChannel(4);        tv.printStatus();
    tv.changeChannel(8);        tv.printStatus();
    tv.changeChannel(5);        tv.printStatus();

    System.out.println("I will change the status of the television");
    tv.powerOff();
    System.out.println("The television is now closed");}}   


 class Television {   //this is the blueprint

    boolean power = true; //create a method for tv powerOnOff 
    int channel = 0;
    int volume = 0;

    void powerOn(){ //method for power On
        power = true;   }
    void powerOff (){//method for power Off
        power = false;  }
    void changeChannel (int newValue){//method to change the channel 
        channel = newValue;}
        void changeVolume (int newValue){ //method to change the volume 
            volume = newValue;}
    void printStatus(){ //printing the status of the Television, channel and volume

    System.out.println("The TV staus is powerOn: " + "Channel: " + channel + " Volume: " + volume); }}
java television
3个回答
2
投票

在伪代码中创建3个方法:

TurnOn()
  power = true;

TurnOff()
  power = false;

IsOn()
  return power;

然后在Main或caller中:

    if(tv.IsOn())
    {
      while(i<5)
        tv.changeChannel(i++);
      tv.changeVolume(x);
    }

2
投票

将此添加到您的电视课程。

void powerOn() {
    power = true;
}

void powerOff() {
    power = false;
}

1
投票

所以,我最后通过添加到类TV中的方法来解决这个问题。参数if(power==true)和我在System.out.println之后调用/调用它的主要方法。

这是我的最终代码:

public class TelevisionDemo {

public static void main(String[] args) {
    //create television object
    Television tv = new Television ();

    //invoke call methods on the object tv
    tv.powerOn();
    tv.powerOff();
    tv.changeChannel (1);
    tv.changeVolume (2);
    tv.printStatus ();

    System.out.println("I will change the the volume one time and the channel 5 times");
    tv.powerOn();               tv.changeVolume(6);
    tv.changeChannel(2);        tv.printStatus();
    tv.changeChannel(3);        tv.printStatus();
    tv.changeChannel(4);        tv.printStatus();
    tv.changeChannel(8);        tv.printStatus();
    tv.changeChannel(5);        tv.printStatus();

    System.out.println("I will change the status of the television");
    tv.powerOff();
    System.out.println("The television is now closed");

}
    }




     //this is the blueprint
 class Television {

    boolean power = true; //create a method for tv powerOnOff 
    int channel = 0;
    int volume = 0;

    void powerOn(){ //method for power On
     power = true;
    }
    void powerOff (){//method for power Off
        power = false;
    }
    void changeChannel (int newValue){//method to change the channel 
        if (power==true)
        channel = newValue; 
 }
        void changeVolume (int newValue){ //method to change the volume 
            if (power==true)
            volume = newValue;
        }
    void printStatus(){ //printing the status of the Television, channel and volume

    System.out.println("The TV staus is powerOn: " + "Channel: " + channel + " Volume: " + volume);
    }
    }
© www.soinside.com 2019 - 2024. All rights reserved.