【问题标题】:How to call a method from a different method within the same class in an if statement如何在 if 语句中从同一类中的不同方法调用方法
【发布时间】: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


【解决方案1】:

您的 step 方法接收 int 方向参数,而您的 canTakeStep 方法没有。只需按照与 step 方法相同的方式传递变量即可:

public boolean canTakeStep(int direction)
{

}

【讨论】:

  • 这需要我定义变量方向,这取决于用户的输入,这是另一种方法的一部分。
  • 是的,否则您将无法在该方法中使用该变量
猜你喜欢
  • 2013-04-20
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
相关资源
最近更新 更多