这是我到目前为止所做的,但是当我编译代码时,它说找不到变量跳转。我写的不正确吗?
完整代码:
public class WizardController
{
/**
* instance variables
*/
private Wizard wizard1;
private Wizard wizard2;
private int numOfRepeats;
private static final int MIN_NUM_REPEATS = 1;
private static final int MAX_NUM_REPEATS = 3;
public static final int LAST_CELL = 11;
/**
* Constructor for objects of class WizardController.
*/
public WizardController(Wizard aWizard1, Wizard aWizard2)
{
super();
this.wizard1 = aWizard1;
this.wizard2 = aWizard2;
this.numOfRepeats = 0;
}
/* instance methods */
/**
* Prompts the user for a number of action repeats
* in the range 1 to 3 inclusive, and returns this number.
*/
public int promptForNumOfRepeats()
{
int moves;
moves = Integer.parseInt(OUDialog.request("Please enter the number of"
+ " action repeats to be performed - between 1 and 3 (inclusive)"));
try
{
moves = Integer.parseInt(OUDialog.request("Please enter the number of"
+ " action repeats to be performed - between 1 and 3 (inclusive)"));
}
/**
* Returns true if the argument is in the range 1 to 3 (inclusive),
* otherwise false.
*/
public boolean isValidNumOfRepeats(int aNumber)
{
return ((aNumber >= WizardController.MIN_NUM_REPEATS)
&& (aNumber <= WizardController.MAX_NUM_REPEATS));
}
/**
* Repeatedly prompts the user for a number of repeats of the moves,
* until they enter a valid input representing a number in the range 1 to 3
* inclusive, and then returns this number.
*/
public int getNumOfRepeats()
{
int repeats = this.promptForNumOfRepeats();
while (!this.isValidNumOfRepeats(repeats))
{
OUDialog.alert("That is not a valid number of game repeats");
repeats = this.promptForNumOfRepeats();
}
return repeats;
}
public void jump(Wizard wizard1)
{
wizard1.up();
this.delay(1000);
wizard1.down();
Wizard w = new Wizard(jump); <<<< not working
}
这是我到目前为止所做的,但是当我编译代码时,它说找不到变量跳转。我写的不正确吗?到目前为止,这是我所做的,但是在编译代码时却说找不到变量跳转。我写的不正确吗?
确实是您得到的错误是正确的。您正在尝试将名为jump的变量传递给Wizard对象的构造函数。
但是,跳转是一种接受向导的方法。因此,您可以在jump方法之外的某处编写以下代码(除非您尝试创建递归性):
public void jump(Wizard wizard1)
{
wizard1.up();
this.delay(1000);
wizard1.down();
}
public void otherMethod() {
Wizard w = new Wizard();
this.jump(w);
}