-5

我正在学习java,所以如果它看起来很基础,请耐心等待。我有一个方法,我试图编辑它以返回一个“读入”的值 - 我试图返回“移动”。但是,由于代码的设置,返回超出了代码块并迫使我返回 null。有人可以编辑代码以使其返回“移动”值吗?我已经为此工作了 2 天,但我无法解决 - 尝试和捕获似乎导致了问题

public Move listenToEngineMove()
{
  synchronized(engineReadBuffer)
  {
    int numRows=engineReadBuffer.size();

    if(numRows==0);

    for(int kk=0; kk<numRows; kk++)
    {
      String row=engineReadBuffer.get(kk);

      row=row.toLowerCase();

      if((row.contains("move "))||(row.contains(" ... ")))
        if((!row.contains("illegal"))&&(!row.contains("error")))
          try { 
            String[] tokens=row.replaceAll("\\<.*\\>"," ").split("\\s+");
            Move move = new Move(tokens[tokens.length-1]);
            jcb.makeAIsMove(move);

            System.out.println("thread.... " + row);
          }  
          catch (Exception x) {
            System.out.println("Exception! : "+x.getMessage());
          } 
    }

    engineReadBuffer.clear();
  }
  return null;
}
4

2 回答 2

4

尝试这个:

public Move listenToEngineMove() {
    Move move = null;
    synchronized (engineReadBuffer) {
        int numRows = engineReadBuffer.size();
        if (numRows == 0) ;   // what on earth is this?
        for (int kk = 0; kk < numRows; kk++) {
            String row = engineReadBuffer.get(kk);
            row = row.toLowerCase();
            if ((row.contains("move ")) || (row.contains(" ... ")))
                if ((!row.contains("illegal")) && (!row.contains("error")))
                    try {
                        String[] tokens = row.replaceAll("\\<.*\\>", " ").split("\\s+");
                        move = new Move(tokens[tokens.length - 1]);
                        jcb.makeAIsMove(move);

                        System.out.println("thread.... " + row);
                    } catch (Exception x) {
                        System.out.println("Exception! : " + x.getMessage());
                    }
        }
        engineReadBuffer.clear();
    }
    return move;
}

我建议你更换这个:

catch(Exception x){System.out.println("Exception! : "+x.getMessage());} 

有了这个:

catch(Exception e){
   e.printStackTrace();   // Or, better yet, logging with Log4J
} 

完整的堆栈跟踪提供了比消息更多的信息。

这条线对我来说似乎是一个错误。最后的分号看起来不合适。

if (numRows == 0) ;   // what on earth is this?

你的代码看起来很糟糕。我觉得很难阅读,因为您与缩进和一般代码样式不一致。风格很重要;它使您的代码更易于阅读和理解。采用更好的风格并坚持下去。

于 2012-06-02T12:07:37.033 回答
2

您需要在同步块内移动“移动”,将其保持在同步块内以保持线程安全很重要。

public Move listenToEngineMove()
{
synchronized(engineReadBuffer)
  {
      Move move =null;

      int numRows=engineReadBuffer.size();
      if(numRows==0);

   for(int kk=0; kk<numRows; kk++)
     {
     String row=engineReadBuffer.get(kk);
     row=row.toLowerCase();

     if((row.contains("move "))||(row.contains(" ... ")))
      if((!row.contains("illegal"))&&(!row.contains("error")))
       try { 
        String[] tokens=row.replaceAll("\\<.*\\>"," ").split("\\s+");
        move = new Move(tokens[tokens.length-1]);

             System.out.println("thread.... " + row);
            }  
       catch(Exception x){System.out.println("Exception! : "+x.getMessage());} 
     }  
   engineReadBuffer.clear();
   return move;//this is inside synchronized block
  }
}
于 2012-06-02T12:12:54.297 回答