1

基本上,在我的大学里,我们得到了一个 Finch 机器人来帮助我们开发 Java 编程技能。但是,我有一个小问题。Uni 使用 Windows 机器,我使用 Mac,基本上我们已经获得了预先编写的代码来测试我们的 Finch 机器人。当我在 WIndows 机器上复制并粘贴代码时,Eclipse 没有给我任何错误,我可以继续测试机器人,但是当我在 Mac 上尝试同样的事情时,我得到 30 个错误!我不知道我做错了什么。

这是代码(我已将错误粘贴在代码下方):

import java.util.Random;
import javax.swing.JOptionPane;
import edu.cmu.ri.createlab.terk.robot.finch.Finch;

public class DoesMyFinchWork 
   {
//This value is the time for most of the tests in milliseconds, 1000 = 1 second
//Change this value if the tests are too long or short
final private static int testtime = 5000;
//This is the Finch object
private static Finch myf = null;
//This is the starting point of the testing program
public static void main(String args[]) 
{
    String s = "";
    //'myf' is the name of our Finch object
    //This will used throughout the program to control your Finch and report it's status  
    myf = new Finch();
    do
    {
        //Run the menu until quit or cancel is selected
        s = FinchMenu();
        if (s == "Light Test") RunLightTest(s);
        if (s == "Tilt Test") RunTiltTest(s);
        if (s == "Tap Test") RunTapTest(s);
        if (s == "Temperature Test") RunTemperatureTest(s);
        if (s == "Obstacle Test") RunObstacleTest(s);
        if (s == "Acceleration Test") RunAccelerationTest(s);
        if (s == "Left Wheel Test") RunLeftWheelTest(s);
        if (s == "Right Wheel Test") RunRightWheelTest(s);
        if (s == "Buzzer Test") RunBuzzerTest(s);
        if (s == "Nose Test") RunNoseTest(s);
    } while (s != "Quit"); 
    System.out.println("Exiting DoesMyFinchWork...");
    myf.quit();
}
//This creates the Finch menu
private static String FinchMenu()
{
    Object[] possibilities = {"Light Test", "Tilt Test","Tap Test","Temperature Test", "Obstacle Test","Acceleration Test","Left Wheel Test","Right Wheel Test","Buzzer Test","Nose Test","Quit"};
    String s = (String)JOptionPane.showInputDialog(null,"Dr Swift's Splendid Finch Test\n++++++++++++++++++++++++\nChoose a test from:\n\n","Week Zero Laboratory",JOptionPane.PLAIN_MESSAGE, null,possibilities,"Light Test");
    if (s == null || s.length() == 0) s = "Quit";
    return(s);
}
//Run the light sensor test
//Displays the left and then the right sensor output
private static void RunLightTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    long before = System.currentTimeMillis();
    while(System.currentTimeMillis() - before < testtime)
    {
        System.out.println(myf.getLeftLightSensor() + " " + myf.getRightLightSensor());
    }
}
//Run the Tilt Test
//Displays:
//1) Is the beak down?
//2) Is the beak up?
//3) Is the Finch level?
//4) Is the Finch upside down?
//5) Is the Finch's left wing down?
//6) Is the Finch's right wing down?
private static void RunTiltTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    long before = System.currentTimeMillis();
    while(System.currentTimeMillis() - before < testtime)
    {
        System.out.println(myf.isBeakDown() + " " + myf.isBeakUp() + " " + myf.isFinchLevel() + " " + myf.isFinchUpsideDown() + " " + myf.isLeftWingDown() + " " + myf.isRightWingDown());
    }
}
//Run the tap test
//Displays if the Finch has been tapped
private static void RunTapTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    long before = System.currentTimeMillis();
    while(System.currentTimeMillis() - before < testtime)
    {
        System.out.println(myf.isTapped());
    }
}
//Run the temperature test
//Displays the current temperature in degrees Celsius
private static void RunTemperatureTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    long before = System.currentTimeMillis();
    while(System.currentTimeMillis() - before < testtime)
    {
        System.out.println(myf.getTemperature());
    }
}
//Run the obstacle sensor test
//Displays if there is an obstacle left and right of the Finch
private static void RunObstacleTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    long before = System.currentTimeMillis();
    while(System.currentTimeMillis() - before < testtime)
    {
        System.out.println(myf.isObstacleLeftSide() + " " + myf.isObstacleRightSide());
    }
}
//Run the acceleration sensor test
//Displays is the Finch is being shaken, and then the acceleration in the X, Y and Z planes
private static void RunAccelerationTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    long before = System.currentTimeMillis();
    while(System.currentTimeMillis() - before < testtime)
    {
        System.out.println(myf.isShaken()+ " " + myf.getXAcceleration() + " " + myf.getYAcceleration()+ " " + myf.getZAcceleration());
    }
}
//Run the left wheel test
//Move the left wheel forward and backwards
private static void RunLeftWheelTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    myf.setWheelVelocities(255,0,testtime/2);
    myf.setWheelVelocities(-255,0,testtime/2);
}
//Run the right wheel test
//Move the right wheel forward and backwards
private static void RunRightWheelTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    myf.setWheelVelocities(0,255,testtime/2);
    myf.setWheelVelocities(0,-255,testtime/2);
}
//Sound the buzzer for a number of different frequencies
private static void RunBuzzerTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    for(int i=0;i<=5000;i+=10)
    {
        myf.buzz(i,10);
        long before = System.currentTimeMillis();
        while(System.currentTimeMillis() - before < 10)
        {
            //Do nothing...
        }
    }
}
//Flash the Finch's nose red, green and blue
//Then flash it randomly
private static void RunNoseTest(String s)
{
    System.out.println("\n"+"Running: "+s+"\n");
    for(int r=0;r<=255;r+=5)
    {
        myf.setLED(r,0,0,10);
    }
    for(int g=0;g<=255;g+=5)
    {
        myf.setLED(0,g,0,10);
    }
    for(int b=0;b<=255;b+=5)
    {
        myf.setLED(0,0,b,10);
    }
    Random rand = new Random();
    rand.setSeed(System.currentTimeMillis());
    for(int i=0;i<50;++i)
    {
        int r = Math.abs(rand.nextInt() % 255);
        int g = Math.abs(rand.nextInt() % 255);
        int b = Math.abs(rand.nextInt() % 255);
        myf.setLED(r,g,b,5);
    }
}

}

以下是一些错误(所有 30 个错误都说“Finch 无法解析为一种类型”):

Description Resource    Path    Location    Type
Finch cannot be resolved to a type  DoesMyFinchWork.java    /DoesMyFinchWork/src    line 169    Java
Problem          
Finch cannot be resolved to a type  DoesMyFinchWork.java    /DoesMyFinchWork/src    line 165    Java   
Problem
Finch cannot be resolved to a type  DoesMyFinchWork.java    /DoesMyFinchWork/src    line 161    Java  
Problem
Finch cannot be resolved to a type  DoesMyFinchWork.java    /DoesMyFinchWork/src    line 146    Java

非常感谢你的帮助!

4

3 回答 3

2

看起来 Windows 机器已经完成了一些配置,因此它知道在哪里可以找到这个Finch类,而你的 Mac 不知道。相当肯定 Windows 与 Mac 的事情在这里是一个红鲱鱼:如果你去发现另一台(未配置的)Windows 机器,它就不会工作。

您可能需要下载一个包含Finch该类的库,然后在 Eclipse 中配置找到它的位置。获取这些信息的最佳位置可能是课堂笔记/网站。如果这些都没有,那就问教授。

于 2012-10-04T16:44:53.137 回答
0

(编辑:哦,该死。没有阅读确切的问题......但如果你以后有问题......)

尝试更换

(s == "光照测试")

(s.equals("光照测试"))

等等。

'JOptionPane.showInputDialog' 可能在不同平台上以不同的代码实现。要使“==”正常工作,对话框必须返回与发送的可能性参数中相同的字符串对象(也需要“实习”,但我认为它确实是实习字面量)。HTH。

于 2012-10-04T16:47:25.073 回答
0

在eclipse中只需找到Finch的jar,将其复制粘贴到您项目的文件夹中(希望您有一个lib文件夹),右键单击该文件构建路径>添加到构建路径

那应该可以解决问题。

于 2012-10-04T17:36:10.027 回答