首页 > 解决方案 > 我希望能够“打开”视频游戏机,但前提是电视频道设置为 3。我该怎么做?

问题描述

不要介意我可怕代码的其他部分,仍在学习中。我只是想通过我的“控制台”类创建一个“playGame”方法,但前提是我的“电视”类的“频道”设置为 3。

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

这是我的课程:

控制台.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;
        }
    }

电视.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;
        }
    }

编辑(添加主类和输出)

主.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.

标签: javaclass

解决方案


发生这种情况是因为 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 实例的引用传递给控制台。

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

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

//Main
console.setTV(tv);

推荐阅读