我昨天问了这个问题,试图实现我收到的最佳答案.所以从昨天的代码我尝试添加synchronized
到我的方法和使用wait()
和notifyAll()
.我一直在查看一堆示例,并阅读文档,但我肯定没有做对.
基本上,一旦touchEvent发生,我将ButtonListener
停止执行所有其他代码,并且只执行包含在其中的代码ButtonListener
.这只发生在我的一台计算机上,我的笔记本电脑按照我预期的方式运行代码,我的桌面卡在了ButtonListener
.这是我几周前转学的一项学校作业,最初收到了70%,但我向老师解释说,它可以在某些电脑上运行,然后在办公室桌面上运行,中提琴,它有效,我得到100 %.当然我想弄清楚问题是什么,所以这就是为什么我还在努力解决这个问题.
以下是有问题的代码片段:
public synchronized void playOneTurn(int player)
throws InterruptedException {
waiting = true;
while (waiting) {
try {
System.out.println("playOneTurn before wait.");
wait();
} catch (InterruptedException e) {
// e.printStackTrace();
}
}
System.out.println("playOneTurn AFTER wait.");
}
Run Code Online (Sandbox Code Playgroud)
我的方法playOneTurn
是在第一次触摸事件之前运行的最后一段代码.以前,你可以通过查看我在顶部链接的问题来判断,我使用了一个简单的while
循环,等待我ButtonListener
翻转一个布尔值,waiting
.以上是我尝试使用的synchronized
.
class ButtonListener implements ActionListener {
@Override
public synchronized void actionPerformed(ActionEvent e) {
System.out.println("Entering Action Performed");
for …
Run Code Online (Sandbox Code Playgroud) 我的老师给了我这个main.m,我必须编写.h和.m方法.在我的.m(非主要)文件中,我收到了来自Xcode的"不完整实现"警告.我为所有被调用的方法制定了方法,所以我无法弄清楚它为什么这么说.以下是我无法修改的代码:
#import <Foundation/Foundation.h>
#import "ChutesAndLadders.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
ChutesAndLadders *cl = [[ChutesAndLadders alloc]init];
[cl initBoard];
[cl makeChutes:10];
[cl makeLadders:10]
int chutes=0;
int ladders=0;
for(int i=0;i<cl.board.count;i++){
NSString * cell = (NSString *)[cl.board objectAtIndex:i];
int additionalSpaces = (int)[cl addToMove:cell];
if(additionalSpaces>0)
ladders++;
else if (additionalSpaces<0)
chutes++;
}
[cl printBoard];
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我编码的.h,我相信它没关系:
#import <Foundation/Foundation.h>
@interface ChutesAndLadders : NSObject{
@private
NSMutableArray * board;
}
@property (readwrite, retain) NSMutableArray *board;
-(id) initBoard;
-(NSString *)addToMove: (NSString *) …
Run Code Online (Sandbox Code Playgroud) 如果我的某些方法中有一些代码最多包含9个if
语句,并且所有这些语句中的大部分都只能满足其中一个if
要求,那么else if
我的代码会使用加速吗?
例如,如果我有这个
if (x == 1){
do something;
}
if (x == 2){
do something;
}
if (x == 3){
do something;
}
if (x == 4){
do something;
}
Run Code Online (Sandbox Code Playgroud)
会改变它来加速代码吗?
if (x == 1){
do something;
}
else if (x == 2){
do something;
}
else if (x == 3){
do something;
}
else if (x == 4){
do something;
}
Run Code Online (Sandbox Code Playgroud)
在我的实际代码中,if
语句正在评估比整数更复杂的东西.
我有一个简单的、大小不一的井字棋应用程序,它在不同的机器上工作方式不同。在我的笔记本电脑上,它像我想要的那样工作,而在我的台式机上,它卡在ButtonListener
实现ActionListener
. 我很确定这是因为我使用while
循环等待执行操作的方式。这是有问题的代码片段。
public void playOneTurn(int player) {
waiting = true;
while (waiting) {
// Do nothing, touchEvent will change waiting.
}
}
Run Code Online (Sandbox Code Playgroud)
我的意思是让这个方法简单地等待一个有效的点被选择并waiting
在ButtonListener
:
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("TOUCHEVENT");
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
if (e.getSource() == cells[i][j]) {
if (model[i][j] == 'e') {
cells[i][j].setText("");
currentSpot[0] = i;
currentSpot[1] = j;
if (count …
Run Code Online (Sandbox Code Playgroud)