-2

我不知道为什么这总是编译说它缺少返回语句,如果有人可以帮助我,那就太好了!我已经尝试了几个小时,但仍然不明白为什么这段代码无法编译。我对 java 不是很好,但这段代码对我来说看起来很完美。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import net.xotiksys.api.ActiveScript;
import net.xotiksys.api.Manifest;
import net.xotiksys.api.methods.input.Mouse;
import net.xotiksys.api.methods.recognition.ColorRecognition;
import net.xotiksys.api.painting.Paintable;
import net.xotiksys.api.util.Time;

@Manifest(author = "iwrestledabearonce", description = "Auto Miner with bank support", name = "MiningBot", version = 1.0)
public class MiningBot extends ActiveScript implements Paintable {

    private int Mined;
    private Color ITEM = new Color(0, 0, 0);
    private static final Color addy = new Color(74, 93, 74);
    private static final Rectangle magicTab = new Rectangle(629, 170, 28, 30);
    private static final Rectangle inv = new Rectangle(551, 206, 185, 255);
    private static final Rectangle GFrame = new Rectangle(2, 3, 517, 339);
    private static final long START_TIME = System.currentTimeMillis();
    private Point ROCK = null;
    private Point rockToMine = null;
    private String Status = "Starting Auto Miner by iwrestledabearonce - Version 1.0 - Please be patient...";
    private String TempColor = "";

    @Override
    public int script() {
        Time.sleep(700);
        if (ROCK != null) {
            startMining(); //by iwrestledabearonce
        }
        else
            return 700;
    }

    public void startMining() {
    do {
            ROCK = ColorRecognition.getPoint(addy, 2, inv);
            rockToMine = ColorRecognition.getPoint(ITEM, 2, inv);
                if (ROCK != null) {
                    Mouse.click(ROCK, 0, 0, true);
                    Time.sleep(700);
                    Mined++;
                    Status = "Mining Ore...";
                    stopScript();
            } while (ROCKS != null && validate());
    }



    public void paint(final Graphics g) {
        g.setColor(Color.ORANGE);
        int ycoord = 349;
        g.drawString("Status: " + Status, 15, ycoord += 10);
        g.drawString("Rock's Mined: " + Mined, 15, ycoord += 20);
        g.drawString("Run Time: "+ Time.format(System.currentTimeMillis() - START_TIME),15, ycoord += 30);
    }
}

上面的新代码现在会在下面创建此错误:

Scripts\MiningBot.java:52: error: while expected
        }
         ^
Scripts\MiningBot.java:56: error: illegal start of expression
        public void paint(final Graphics g) {
               ^
Scripts\MiningBot.java:56: error: ')' expected
        public void paint(final Graphics g) {
                   ^
Scripts\MiningBot.java:56: error: ';' expected
        public void paint(final Graphics g) {
                         ^
Scripts\MiningBot.java:56: error: ';' expected
        public void paint(final Graphics g) {
                                          ^
Scripts\MiningBot.java:63: error: reached end of file while parsing
}
 ^
6 errors

C:\Users\TJ\Desktop\XotikColor>pause
Press any key to continue . . .
4

6 回答 6

1

问题出在script方法上。您应该始终返回一个值。我正在展示编译器如何读取您的代码:

public int script() {
    Time.sleep(700);
    if (ROCK != null) {
        startMining(); //by iwrestledabearonce
    } else {
        return 700;
    }
    //where's the result?
}

为了使这个工作,改变方法来返回一些东西。一种方法是elseif代码块中删除 ,如下所示:

public int script() {
    Time.sleep(700);
    if (ROCK != null) {
        startMining(); //by iwrestledabearonce
    }
    return 700;
}
于 2013-01-20T06:00:51.530 回答
0

问题出现是因为,if (ROCK!= null )isfalse 700会被返回,iftrue会返回什么???所以,还要return在里面添加一条语句if block

public int script() {
        Time.sleep(700);
        if (ROCK != null) {
            startMining(); //by iwrestledabearonce
        }
        else
            return 700;
    }
于 2013-01-20T06:07:19.920 回答
0

编辑:看起来你在你的程序中粘贴了一个额外的 { 或 },这把整个事情都扔掉了。撤消您所做的任何更改,在 return(700) 之后通过 } 从 public int script() 中删除整个块并粘贴:

public int script() {
    Time.sleep(700);
    if (ROCK != null) {
        startMining(); //by iwrestledabearonce
    } else {}
    return 700;
}
于 2013-01-20T06:16:01.437 回答
0

在这里,您仅在 else 块而不是 if 块的情况下返回:

public int script() {
        Time.sleep(700);
        if (ROCK != null) {
            startMining(); //by iwrestledabearonce
        }
        else
            return 700;
    }

您应该从所有可能的分支返回一些值,而不仅仅是一个

于 2013-01-20T06:00:34.783 回答
0

失去else

    public int script() {
        Time.sleep(700);
        if (ROCK != null) {
            startMining(); //by iwrestledabearonce
        } // ELSE REMOVED
        return 700;
    }
于 2013-01-20T06:01:17.530 回答
0

这是因为您开始了 else 语句并且没有关闭它。要么删除 else 语句,要么这样做:

    public int script() {
        Time.sleep(700);
        if (ROCK != null) {
            startMining(); //by iwrestledabearonce
        } else {}
        return 700;
于 2013-01-20T06:03:02.150 回答