부울 메서드의 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 메서드가 void가되어야합니다. 또한, 나는 상수가 단계 메서드에 대해 설정되어 있으므로 텍스트 문자열로 인수를 처리하는 것처럼 보이지만 정말 정수로 있습니다. – confusedcsstudent