【发布时间】:2017-01-26 12:06:28
【问题描述】:
我试图在布尔方法的 if 语句中调用 step() 方法的方向,但它一直说找不到变量方向。谁能帮我弄清楚如何正确地做到这一点?
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("+") ;
}
}
这里是带有main方法的驱动类:
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方法,因此也应该定义它,而不仅仅是声明。 -
另请注意,在
canTakeStep方法中,您期望step方法调用返回一些东西,但后者实际上并没有返回任何东西。而且,step方法期望参数是一个定义的整数,但你正在处理它,就像是一个文本字符串。 -
@Hiren 它不允许我在任何地方定义变量方向,因为它已经在 step 方法中声明,并且 step 方法在这种情况下必须是无效的。另外,我为 step 方法设置了常量,所以看起来我将参数作为文本字符串处理,但它实际上是整数。
-
是的,它已在
step方法中声明为参数,但您应该在对step的调用中简单地传递实际的整数/字符串本身,而不是使用step(direction),例如step(1)或step("NORTH")等。即使在这种情况下,它实际上也不会返回任何东西。那么你将无法在canTakeStep方法中进行比较。 -
@Hiren 我明白你的意思......让我解释一下我正在尝试做什么。我正在使用二维字符数组。该类是 SelfAvoidingRandomWalk。我们需要用户输入行数和列数,然后输入起始行和起始列。然后我们需要记录并打印移动者的每一步,(NORTH = 8,SOUTH = 2,WEST = 4,EAST = 6),如果他们遇到一个被占用的“单元格”,那么我们输出步行的长度。我们的教授为我们提供了示例代码供我们使用,我们必须坚持他给我们的东西。他的示例中的 step 方法的返回类型为 void。
标签: if-statement methods call