变量声明问题

时间:2013-11-26 00:59:48

标签: java

我无法弄清楚为什么eclipse要我删除“;”并用“,”替换它。

numberOfTimes1 = numberOfTimes + numberOfDelayTimes;

我猜这是我忘记的一些简单的语法。你能解释它为什么这样做以及如何解决它。 整个计划

public class Spam {


    public static void main(String[] args) {
        //1- Taking an instance of Timer class.
        Timer timer = new Timer("Printer");

        //2- Taking an instance of class contains your repeated method.
        timeso t = new timeso();

        timer.schedule(t, 0, 10);
    }

}
class timeso extends TimerTask {
    //times member represent calling times.
    private int times = 0;
    int time = 6; //How long do you wish for the spamming to run?
    int numberOfTimes = time * 100;
    int delayTime = 5; //How long do you wish for the program to wait before spamming?
    int numberOfDelayTimes = delayTime * 100;
    numberOfTimes = numberOfTimes + numberOfDelayTimes;
    String spam;
    Random randomGenerator = new Random();

    public void run() {
        times++;
        if (times >= numberOfDelayTimes && times <= numberOfTimes+numberOfDelayTimes) {

            try {
                Robot typer = new Robot();
                //for(int x = 1;x <=randomGenerator.nextInt(5); x++){
                //  spam = spam + randomGenerator.nextInt(10);
                //}
                byte[] bytes = "spam".getBytes();
                //byte[] bytes = spam.getBytes();
                for (byte b : bytes){
                    int code = b;
                    // key code only handles [A-Z] (which is ASCII decimal [65-90])
                    if (code > 96 && code < 123) code = code - 32;
                    typer.delay(10/bytes.length+1);
                    typer.keyPress(code);
                    typer.keyRelease(code);
                }
                if(times % (randomGenerator.nextInt(25)+1) == 0){
                    typer.delay(10/bytes.length+1);
                    typer.keyPress(KeyEvent.VK_ENTER);
                    typer.keyRelease(KeyEvent.VK_ENTER);
                }
            } 
            catch (AWTException e){
            } 
        } else {
            if (times >= numberOfTimes){
                try{
                    Robot typer = new Robot();
                    typer.delay(10);
                    typer.keyPress(KeyEvent.VK_ENTER);
                    typer.keyRelease(KeyEvent.VK_ENTER);
                } catch(Exception e){

                }
                //Stop Timer.
                this.cancel();
            }
        }
}   
    }  

1 个答案:

答案 0 :(得分:3)

你试图在方法或构造函数之外调用一行代码,这就是Java编译器(而不是Eclipse)抱怨的原因。在构造函数或方法中执行那种代码,而不是在类中裸。实际上,你的timeso类中的所有代码都是错误的,需要在方法或构造函数中。

注意:您将学习Java命名约定并坚持使用它,包括使用大写字母和方法启动类名以及使用小写字母的变量名。这样做有助于其他人(我们!)更好地理解您的代码。