- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
不要介意我可怕的代码的其他部分,仍在学习。我只是想通过我的“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/
我目前正在开发一个 Android 电视应用程序,但无法弄清楚如何更改当我在播放视频时离开该应用程序时出现的正在播放的卡片的图标。 这link显示我需要使用 uri 格式的字符串设置 MediaMet
[注意:这是对 earlier question 的重写这被认为是不合适的和封闭的。] 我需要对电视 (TV) 视频进行一些像素级分析。这种分析的确切性质并不相关,但它基本上涉及查看电视视频每一帧的每
我长期使用 mumudvb 和一张 dvb-t pci 卡在我的网络上流式传输电视 channel 。这工作正常,但我最近添加了另一个不如其他播放器强大的播放器,所以我想重新编码视频流。 这是我的输入
我正在尝试为 android 电视制作一个应用程序,它将使用电视 Remote 上的以下按钮:up、down、left,向右,居中/输入,home,返回。 我需要参加哪些类(class)/Activi
Paypal Rest Api 不适用于 Tizen tv,但它在 rest 客户端上运行良好。Tizen tv 中既不调用成功函数也不调用失败函数。 更改了 paypal 以外的 url,它的工作正
我想访问以下当前可用推送通知的数量: ,这里是 Android TV 模拟器图像.. 那么我怎样才能读取那个数字呢? 编辑:我不想设置任何东西。 最佳答案 您可以使用 NotificationList
我知道如何在显示键盘时在 android 移动设备中进行检测,但是如何在 android 电视或火电视上做到这一点? 最佳答案 尝试这个 fun isKeyBoardVisible() = ViewC
我有一台 3D 电视,并且觉得如果我不至少尝试让它显示我自己创作的漂亮 3D 图像,我就是在逃避自己的责任(作为极客)! 我之前已经完成了非常基本的 OpenGL 编程,因此我了解所涉及的概念 - 假
我想通过 PC 上的 Java 程序控制我的 DLNA 电视。特别是,我想: 发现局域网中是否有电视 检查它当前是否开启(第一步) 稍后向它发送一些命令(例如关闭) 我相信,我需要实现数字媒体 Con
我看到很多帖子都在展示如何使用 adb connect 连接到 Android 设备但就我而言,它不起作用... unable to connect to :5555: cannot connect
我想从一个 java 程序发送以下命令,但并不过分阅读响应。知道我该怎么做吗 下面的命令通过 CEC 命令打开电视 echo "standby 0000" | cec-client -d 1 -s "
我有一台每天自动启动的独立公共(public)信息亭电脑。它连接到高清电视,有时检测不到。我必须亲自去 PC,转到屏幕分辨率,然后按检测它的工作原理。 我的问题是如何知道我希望它显示的显示器是否已在代
如何在 Erlang/OTP v17 中查看 mnesia 表。我尝试使用 tv:start() 但外壳返回 undefined function。看起来 tv 模块不再存在。用什么来代替它? 最佳答
我目前遇到了 Facebook 实时 API 的问题。 我希望订阅用户个人资料中的一些内容,包括他们在音乐、书籍、电视和电影类别中的“喜欢”。 当我通过 FQL 和 Graph 查询时,我得到了正确的
我是一名优秀的程序员,十分优秀!