我在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);
}
}
我创建了一个方法powerOn
,powerOff
并在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); }}
答案 0 :(得分:2)
在伪代码中创建3个方法:
TurnOn()
power = true;
TurnOff()
power = false;
IsOn()
return power;
然后在Main或来电者中:
if(tv.IsOn())
{
while(i<5)
tv.changeChannel(i++);
tv.changeVolume(x);
}
答案 1 :(得分:2)
将此内容添加到您的电视课程中。
void powerOn() {
power = true;
}
void powerOff() {
power = false;
}
答案 2 :(得分:1)
所以,我最后通过添加到课堂电视中的方法来解决这个问题。参数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);
}
}