在Robocode上的Java中,如何解决“语法错误,仅当源级别为1.5或更高级别时,静态导入才可用”

时间:2018-10-14 21:38:56

标签: java robocode

我正在尝试在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; 
}                                                                                          

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

Window Types

您可以尝试很多方法。

希望获得帮助。

答案 2 :(得分:0)

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

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