如何从if语句中的同一个类中的不同方法调用方法

时间:2016-09-18 20:26:12

标签: if-statement methods call

我试图在boolean方法的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() ;
    }
}

1 个答案:

答案 0 :(得分:0)

您的步骤方法接收int direction参数,而canTakeStep方法则不接收。只需按照您在步骤方法上执行的操作传递变量:

public boolean canTakeStep(int direction)
{

}