java - 我希望能够 "turn on"视频游戏机,但前提是电视 channel 设置为 3。我该如何执行此操作?

标签 java class

不要介意我可怕的代码的其他部分,仍在学习。我只是想通过我的“Console”类创建一个“playGame”方法,但前提是我的“TV”类的“ channel ”设置为 3。

我的程序说没有任何错误,但是当我运行程序并将 channel 设置为 3 时,我仍然无法玩游戏。我不断收到 println“您需要将 channel 更改为 3 才能播放。”

这是我的类(class):

Console.java

    public class Console {

        TV tv = new TV(5,25);

        private boolean isOn;
        private String reset;

        public Console(boolean isOn, String reset) {
            super();
            this.isOn = isOn;
            this.reset = reset;
        }

        public void powerButton() {
            if (isOn) {
                System.out.println("You turned off your video game console.");
                isOn = false;
            } else {
                System.out.println("You turned on your video game console.");
                isOn = true;
            }

        }

        public void reset () {
            System.out.println("You reset your game console.");
        }

        public void playGame() {
            if (tv.getChannel() == 3) {
                System.out.println("You play a game on your console.");
            } else {
                System.out.println("You need to change the channel to 3 before you can play.");
            }

        }

        public boolean isOn() {
            return isOn;
        }
        public String getReset() {
            return reset;
        }
    }

TV.java

    public class TV {
        private int channel = 10;
        private int volume = 25;
        public TV(int channel, int volume) {
            super();
            this.channel = channel;
            this.volume = volume;
        }

        public void channelUp () {
            channel++;
            System.out.println("The channel is now on channel " + channel);
        }

        public void channelDown () {
            channel--;
            if (channel < 0) {
                channel = 0;
            }
            System.out.println("The channel is now on channel " + channel);
        }

        public void volumeUp () {
            volume++;
            System.out.println("You changed the volume to " + volume);
        }

        public void volumeDown () {
            volume--;
            if (volume < 0) {
                volume = 0;
            }
            if (volume > 100) {
                volume = 100;
            }
            System.out.println("You changed the volume to " + volume);
        }

        public void currentChannel() {
            int currentChannel = channel;
            System.out.println("The current channel is " + channel);
        }

        public void changeChannel(int changeToChannel) {
            channel = changeToChannel;
            System.out.println("You changed the channel to " + channel);
        }

        public int getChannel() {
            return channel;
        }
        public int getVolume() {
            return volume;
        }
    }

编辑(添加主类和输出)

Main.java

public class Main {

    public static void main (String[] args) {
//GameRoom
        Light light = new Light ("Modern");
        Macbook macbook = new Macbook ("2013", "Aluminum");
        TV tv = new TV (25,50);
        Bed bed = new Bed ("Double",3,2);
        Console console = new Console (false, "reset");

        light.turnOn();
        light.getStyle();
        light.turnOff();

        macbook.macbookDetails();

        tv.channelDown();
        tv.channelDown();
        tv.channelDown();
        tv.volumeUp();
        tv.volumeUp();

        bed.make();
        bed.messUp();
        macbook.playGame();
        macbook.turnOn();
        macbook.playGame();
        macbook.turnOff();

        console.powerButton();
        tv.currentChannel();
        tv.changeChannel(5);
        console.playGame();
        tv.changeChannel(3);
        console.playGame();
    }
}

输出

You turned on the light in the GameRoom
Modern style lamp.
You turned off the light in the GameRoom
--Macbook details--
Year: 2013
Color: Aluminum
The channel is now on channel 24
The channel is now on channel 23
The channel is now on channel 22
You changed the volume to 51
You changed the volume to 52
You made your bed.
You messed up your bed.
You need to turn on your Macbook first.
Macbook turned on.
You played Celeste on your Macbook.
Macbook turned off.
You turned on your video game console.
The current channel is 22
You changed the channel to 5
You need to change the channel to 3 before you can play.
You changed the channel to 3
You need to change the channel to 3 before you can play.

最佳答案

发生这种情况是因为 TV 类有两个不同的实例。

控制台中的一个

public class Console {

    TV tv = new TV(5,25);

入口点有一个

public static void main (String[] args) {
    //GameRoom
    Light light = new Light ("Modern");
    Macbook macbook = new Macbook ("2013", "Aluminum");
    TV tv = new TV (25,50);

因此,当您在入口点中引用 tv.changeChannel(3) 时,这是与在控制台中引用 tv.getChannel() 时不同的 TV 实例。

要解决此问题,您需要将在入口点中创建的 TV 实例的引用传递到控制台。

我将创建一个 setter 方法,将 TV 对象提供给控制台,然后从入口点调用该方法。

//Console
public void setTV(TV tv) {
    this.tv = tv;
}

//Main
console.setTV(tv);

关于java - 我希望能够 "turn on"视频游戏机,但前提是电视 channel 设置为 3。我该如何执行此操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60009781/

相关文章:

java - 如何修复 'android.os.NetworkOnMainThreadException' ?

java - Axon - MySql 的 JPA 事件存储

java - 在JAVA中使用WatchService。我可以同时观看子目录吗?

class - UML 类图枚举

c++ - 一个类是 POD 从二进制文件中读取它就足够了吗?

java - 如何提取和替换具有特定格式的字符串?

java - 在 struts 2 应用程序中没有 Action 映射错误

swift - 用于创建类实例或创建空变量的 If/Switch 语句

c++ - 用另一个类成员 C++ 初始化一个类成员

javascript - 如何通过类名或另一个类名获取元素?