我试图从布尔方法中的if语句中的step()方法调用方向,但它一直说它找不到可变方向。任何人都可以帮我弄清楚如何正确地做到这一点?如何调用if语句中相同类中的不同方法
public class SelfAvoidingRandomWalk
{
private char[][] board ;
private char symbol ;
private int lengthOfWalk, currRow, currColumn ;
public SelfAvoidingRandomWalk(int numRows, int numColumns,
int startRow, int startColumn)
{
board = new char[numRows][numColumns] ;
lengthOfWalk = 0 ;
symbol = '$' ;
board[startRow][startColumn] = symbol ;
currRow = startRow ;
currColumn = startColumn ;
}
private final static int NORTH = 8 ;
private final static int SOUTH = 2 ;
private final static int WEST = 4 ;
private final static int EAST = 6 ;
public void step(int direction)
{
if (direction == NORTH)
{
if (currRow == 0)
{
currRow = board.length ;
}
currRow-- ;
symbol = 'N' ;
}
else if (direction == SOUTH)
{
currRow = (currRow + 1) % board.length ;
symbol = 'S' ;
}
else if (direction == EAST)
{
currColumn = (currColumn + 1) % board[0].length ;
symbol = 'E' ;
}
else if (direction == WEST)
{
if (currColumn == 0)
{
currColumn = board[0].length ;
}
currColumn-- ;
symbol = 'W' ;
}
board[currRow][currColumn] = symbol ;
}
public boolean canTakeStep()
{
boolean stepOk = true ;
if (board[currRow + 1][currColumn] == symbol)
{
if (step(2) == SOUTH)
{
stepOk = false ;
}
}
if (board[currRow - 1][currColumn] == symbol)
{
if (step(8) == NORTH)
{
stepOk = false ;
}
}
if (board[currRow][currColumn + 1] == symbol)
{
if (step(6) == EAST)
{
stepOk = false ;
}
}
if (board[currRow][currColumn - 1] == symbol)
{
if (step(4) == WEST)
{
stepOk = false ;
}
}
return stepOk ;
}
public int length()
{
if (canTakeStep() == true)
{
lengthOfWalk++ ;
}
else
{
System.out.println("The length of your walk was: " + lengthOfWalk) ;
}
return lengthOfWalk ;
}
public void print()
{
printTopBottom() ;
for (int r = 0; r < board.length; r++)
{
System.out.print("|") ;
for (int c = 0; c < board[r].length; c++)
{
if (symbol == '$' || symbol == 'N' || symbol == 'S' || symbol == 'W' || symbol == 'E')
{
if (board[r][c] != symbol)
{
System.out.print(" ") ;
}
else
{
System.out.print(symbol) ;
}
}
}
System.out.println("|") ;
}
printTopBottom() ;
}
private void printTopBottom()
{
System.out.print("+") ;
for (int c = 0; c < board[0].length; c++)
{
System.out.print("-") ;
}
System.out.println("+") ;
}
}
这里是驱动程序类的主要方法:
import java.util.Scanner ;
public class Lab2
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in) ;
System.out.print("Enter number of rows: ") ;
int numRows = input.nextInt() ;
System.out.print("Enter number of columns: ") ;
int numColumns = input.nextInt() ;
System.out.print("Enter start row: ") ;
int startRow = input.nextInt() ;
System.out.print("Enter start column: ") ;
int startColumn = input.nextInt() ;
System.out.println() ;
SelfAvoidingRandomWalk selfAvoidingRandomWalk = new SelfAvoidingRandomWalk(numRows, numColumns,
startRow, startColumn) ;
selfAvoidingRandomWalk.print() ;
while (selfAvoidingRandomWalk.canTakeStep() == true)
{
System.out.print("Enter the direction you'd like to go: ") ;
int theMove = input.nextInt() ;
selfAvoidingRandomWalk.step(theMove) ;
selfAvoidingRandomWalk.print() ;
}
selfAvoidingRandomWalk.length() ;
}
}
这并不完全清楚你打算做什么。变量'direction'需要在'canTakeStep()'方法中声明,除非它已经在别处被声明为全局变量。实际上,由于您将该变量作为参数传递给'step'方法,因此应该定义它,而不仅仅是声明。 – Hiren
另请注意,在'canTakeStep'方法中,您期待'step'方法调用返回某些内容,但后者实际上不会返回任何内容。此外,'step'方法期望参数是根据定义的一个整数,但您正在处理它,就好像是一个文本字符串。 – Hiren
@Hiren它不允许我在任何地方定义变量方向,因为它已经在step方法中声明了,在这种情况下step方法HAS是无效的。另外,我为step方法设置了常量,所以看起来好像我将参数作为文本字符串处理,但实际上它们是整数。 – confusedcsstudent