我正在模擬沿水平線移動的錯誤的程序,當我在自己的類中測試它時,我的代碼正常工作,但是當我嘗試測試構造函數和測試類中的方法我收到一個錯誤,在我的構造函數Bug中發現「無法找到符號」。我還是新來的Java,所以我不知道我們的錯誤是。 編輯。構造函數和方法在測試類中不工作java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bugtester;
/**
*
* @author Luke
*/
public class BugTester {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Bug juneBug = new Bug(0);
juneBug.move();
juneBug.turn();
juneBug.move();
System.out.println(juneBug.getposition());
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bug;
/**
*
* @author Luke
*/
public class Bug {
/**
* @param args the command line arguments
*/
public int position;
public int turner = 1;
public static void main(String[] args) {
Bug juneBug = new Bug(0);
juneBug.move();
juneBug.turn();
juneBug.move();
juneBug.move();
System.out.println(juneBug.getposition());
}
public Bug(int initialPosition){
position = initialPosition;
turner = 1;
}
public void turn(){
turner = turner + 1;
}
public void move(){
if (turner % 2 == 0){
position = position - 1;
}else{
position = position + 1;
}
}
public int getposition(){
return position;
}
}
你有沒有爲你的班級添加'import'? –
你做過任何研究嗎?當你不知道你在做什麼時,「找不到符號」是一個非常常見的錯誤。 –
我不這麼認爲,我會怎麼做呢? – user3806295