1

我正在尝试在 robocode 上编译机器人,但它说“静态导入仅在源级别为 1.5 或更高版本时可用”

我不明白这是什么意思。我在其他论坛上看到可以更新Java,但我的电脑已经安装了最后一个Java。我能做些什么?

这是我试图编译的代码:

package blir.dev; 

import robocode.*;

import java.awt.Color;

import static robocode.util.Utils.*; 

/** 
* Blixi - a robot by Travis Bruce 
*/
public class Keen_Stalker extends AdvancedRobot { 

//the direction we're moving 
private int dir = 1; 
//are we close to a wall? : are we close to a robot?
private boolean wall = false, rbt = false; 
//enemy's energy 
private double en = 100D; 

/** 
 * run: Blixi's default behavior 
 */
public void run() { 
    setColors(Color.BLACK, Color.DARK_GRAY, Color.DARK_GRAY); 
    setAdjustRadarForRobotTurn(true); 
    setAdjustGunForRobotTurn(true); 
    setAdjustRadarForGunTurn(true); 
    //if (bshot1 != 0) out.println("Main Gun Accuracy: " + (double) bhit1 / bshot1);
    for (;;) { 
        if (getRadarTurnRemaining() == 0) { 
            //should only happen if we lose track of them, which should never happen 
            setTurnRadarRight(Double.POSITIVE_INFINITY); 
        } 
        //check if we're too close to the wall (50 pixels) 
        wall = cw(50, true); 
        execute(); 
    } 
} 

/** 
 * onScannedRobot: What to do when you see another robot 
 */
public void onScannedRobot(ScannedRobotEvent e) { 

    //check if we're too close to the other robot (150 pixels) 
    rbt = d(e.getDistance()); 

    //absolute bearing
    double absBear = getHeadingRadians() + e.getBearingRadians(); 

    //track them with our radar 
    double radarTurn = normalRelativeAngle(absBear - getRadarHeadingRadians()); 
    radarTurn += (radarTurn < 0 ? -1 : 1) * Math.atan(36.0 / e.getDistance()); 
    setTurnRadarRightRadians(radarTurn); 

    //fire power the enemy used
    double enemyPower = en - e.getEnergy();

    if (enemyPower <= 3 && enemyPower > 0 && !cw(75, false)) { 
        //they seem to have fired
        cd(); 
    } 

    //record new energy 
    en = e.getEnergy(); 

    //circle around them if we're close enough, move away if we're too close
    setTurnRightRadians(normalRelativeAngle(e.getBearingRadians() + Math.PI / 2D)); 

    setTurnGunRightRadians(normalRelativeAngle(absBear - getGunHeadingRadians() -
            Math.asin(e.getVelocity() * Math.sin(Math.PI - absBear + e.getHeadingRadians()) / Rules.getBulletSpeed(Rules.MAX_BULLET_POWER)))); 

    //fire! 
    setFire(Rules.MAX_BULLET_POWER); 

    //all power ahead! 
    setAhead(dir * 32);
} 

public void onHitWall(HitWallEvent e) { 
    //out.println("Ahh! A wall!"); 
    cd(); 
}  

private boolean d(double d) { 
    if (d < 150) { 
        //we're close to the robot (within 150 pixels) 
        if (!rbt) { 
            //we're not already escaping the robot, so let's change direction 
            cd(); 
        } 
        return true; 
    } 
    return false; 
} 

private boolean cw(int d, boolean cw) { 
    if (getX() < d || getY() < d || getBattleFieldWidth() - getX() < d || getBattleFieldHeight() - getY() < d) { 
        //we're close to the wall 
        if (!wall && cw) { 
            //we're not already escaping the wall, so let's change direction 
            cd(); 
        } 
        return true; 
    } 
    return false; 
} 

private void cd() { 
    dir = -dir; 
}                                                                                          
4

3 回答 3

1

一个快速的解决方法是删除import static robocode.util.Utils.*(在 Java 1.5 中引入了构造 import static ,请参见此处:https ://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html )并使用Utils.normalRelativeAngle等...

正确的解决方法是找出为什么将源级别设置为低于 1.5

于 2018-10-14T22:33:14.597 回答
0

在 Robocode 编译器选项中添加此注释:-source 1.5

->编译器选项[-deprecation -g -source 1.5 -encoding UTF-8]

于 2019-07-16T11:38:11.867 回答
0

我不确定您是否检查了其他答案,但请检查以下链接。

eclipse 魔术:... 语法错误,可变参数仅在源级别为 1.5 或更高版本时可用

您可以尝试很多事情。

希望这有帮助。

于 2018-10-14T21:54:43.183 回答