Java代码中的电视开机或关机
我在eclipse在线学习中有一个Java类项目,我在理解某些东西方面遇到了问题。在我的书中没有对这种情况的解释,我的老师也没用 我的项目是创建一个对象类电视,从开到关切换到电源,只有当电源打开时,我必须切换频道5次,音量切换1次 我知道我必须创建一个布尔方法Java代码中的电视开机或关机,java,television,Java,Television,我在eclipse在线学习中有一个Java类项目,我在理解某些东西方面遇到了问题。在我的书中没有对这种情况的解释,我的老师也没用 我的项目是创建一个对象类电视,从开到关切换到电源,只有当电源打开时,我必须切换频道5次,音量切换1次 我知道我必须创建一个布尔方法if(power==true),但我不知道如何做到这一点以及如何将它与我的代码结合起来 这是我的密码: public class TelevisionDemo { public static void main(String[]
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);
}
}
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); }}
我已经创建了一个方法
powerOn
,powerOff
,并在main方法中调用/调用了它,但我仍然不知道如何使用所有参数,这些参数只允许我在打开电视电源时更改频道和音量
有人能帮我解决这个问题吗
这是我的密码:
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);
}
}
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); }}
在伪代码中创建3个方法:
TurnOn()
power = true;
TurnOff()
power = false;
IsOn()
return power;
然后在Main或caller中:
if(tv.IsOn())
{
while(i<5)
tv.changeChannel(i++);
tv.changeVolume(x);
}
if(tv.IsOn())
{
而(i在伪代码中创建3种方法:
TurnOn()
power = true;
TurnOff()
power = false;
IsOn()
return power;
然后在Main或caller中:
if(tv.IsOn())
{
while(i<5)
tv.changeChannel(i++);
tv.changeVolume(x);
}
if(tv.IsOn())
{
而(我把这个加到你的电视课上
void powerOn() {
power = true;
}
void powerOff() {
power = false;
}
把这个加到你的电视课上
void powerOn() {
power = true;
}
void powerOff() {
power = false;
}
因此,我最终解决了这个问题,在类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);
}
}
因此,我最终解决了这个问题,在类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);
}
}
实际上,这似乎是一个课堂练习。你真的希望StackOverflow上的人帮你解决家庭作业吗?记住if(power==true)
写得更好if(power)
这不是课堂练习,而是通过在线学习发送给学校的项目。是的,我非常希望Stack Overflow上的人帮助我学习如何解决我的问题,这就是我发布问题的原因。我的老师写信给我,我需要添加if(power==true)。实际上,这似乎是一个课堂练习。你真的希望StackOverflow上的人帮你解决家庭作业吗?记住if(power==true)
编写得更好if(power)
这不是课堂练习,而是通过在线学习发送给学校的项目。是的,我非常希望Stack Overflow上的人帮助我学习如何解决我的问题,这就是我发布问题的原因。我的老师写信给我,我需要添加if(power==true)。谢谢你的回答,我是一个真正的初学者,所以我不知道如何在main方法中调用方法TurnOn-TurnOff,我不知道什么是伪代码,除了它意味着不真实。我已经创建了一个方法powerOn-powerOff,并在main方法中调用/调用它,但我仍然不知道如何使用所有只允许我更改的参数打开电视电源时的e频道和音量。任何人都可以帮我解决这个问题吗?这是我的代码:@ElivanBScho你是什么意思?我答案中的while循环是如何更改频道的示例。谢谢你的回答,我是一个真正的初学者,所以我不知道如何在main方法中调用方法TurnOn-TurnOff,我不知道什么是伪代码,而不是它的意思是不真实的。我已经创建了一个方法powerOn powerOff,并在main方法中调用/调用了它,但我仍然不知道如何使用所有参数,这些参数只允许我在打开电视电源时更改频道和音量。任何人都可以帮我解决这个问题吗?这是我的代码:@ElivanBScho你是什么意思?我的答案中的while循环是如何更改频道的一个示例。