如何使用ArrayList从Java中的另一个类调用方法?

时间:2017-02-10 13:20:37

标签: java arraylist

我是Java的初学者,我们有一个关于Bingo的程序。你必须让用户输入玩家数量,每个玩家都会收到一个棋盘。然后从另一个类调用随机令牌,您必须使用该令牌在板上标记它。我不知道如何弄清楚谁赢了......而Mark()上有错误。有人可以帮忙吗?感谢。

import java.util.Scanner;
import java.util.ArrayList;
public class Bingo 
{
    public static void main(String[] args) 
    {
     Scanner a = new Scanner(System.in);
     System.out.println("Enter number of players: ");
     int num = a.nextInt();
     ArrayList<Board> player = new ArrayList<Board>();
     for(int i = 0; i < num; i++)
       player.add(new Board());
     Token one = new Token();
     int tempNumber = 0, tempLetter = 0;
     boolean win = false;
     while(win = false)
     {
       tempLetter = one.getLetter();
       tempNumber = one.getNumber();

       char letter;
       if(tempLetter == 0)
       letter = 'B';
       else if(tempLetter == 1)
         letter = 'I';
       else if(tempLetter == 2)
         letter = 'N';
       else if(tempLetter == 3)
         letter = 'G';
       else
         letter = 'O';

       System.out.println("Token: " + tempNumber + letter);
       System.out.println();

       win = player.Mark(tempNumber,tempLetter);
     }
  }
}

以下是Board类:

public class Board
{
    private int[][] card;
    private int row, column;
    boolean bingo;
    public Board()
    {
        card = new int[5][5];
        for(int i = 0; i < card.length; i++)
            for(int j = 0; j < card[i].length; j++)
                card[i][j] = (int) (Math.random() * 75 + 1);
        card[2][2] = 0;

        for(int i = 0; i < card.length; i++)
            for(int j = 0; j < card[i].length; j++)
            {
                System.out.print(card[i][j] + " ");
                if(j == 4)
                    System.out.println();
            }
    bingo = false; //False
    row = 0;
    column = 0;

}

public boolean Mark(int x, int y)
{   
    for(int i = 0; i < card.length; i++)
        for(int j = 0; j < card[i].length; j++)
        {
            if(card[i][j] == x)
                if(j == y)
                {
                    card[i][j] = 0;
                    row = i;
                    column = j;
                }

        }
    System.out.println("Mark: ");

    for(int i = 0; i < card.length; i++)
        for(int j = 0; j < card[i].length; j++)
        {
            System.out.print(card[i][j] + " ");
            if(j == 4)
                System.out.println();
        }
    Check();
    return bingo;
}

private void Check()
{
    int flagRow = 0, flagColumn = 0, flagDiagonal = 0;
    int tempRow = 0, tempColumn = 0, tempRow1 = 4, tempColumn1 = 4;
    //Diagonal check
    if(row == column)
    {
        while(bingo == false)
        {
            if(card[tempRow][tempColumn] == 0)
            {
                tempRow++;
                tempColumn++;
                flagDiagonal++;
            }
            else if(card[tempRow1][tempColumn1] == 0)
            {
                tempRow1--;
                tempColumn1--;
                flagDiagonal++;
            }

            if(flagDiagonal == 5)
                bingo = true;
            else
            {
                tempRow = 0;
                tempColumn = 0;
                tempRow1 = 4;
                tempColumn1 = 4;
                flagDiagonal = 0;
            }
        }
    }
    //Row and column check
    for(int i = 0; i < card.length; i++)
        for(int j = 0; j < card[i].length; j++)
        {
            if(card[i][column] == 0) //Column check
                flagColumn++;
            if(card[row][j] == 0) //Row check
                flagRow++;
        }

    if(flagRow == 5)
        bingo = true;
    else if(flagColumn == 5)
        bingo = true;
    else
    {
        flagRow = 0;
        flagColumn = 0;
    }

    System.out.println("\nCheck: ");
    for(int i = 0; i < card.length; i++)
        for(int j = 0; j < card[i].length; j++)
        {
            System.out.print(card[i][j] + " ");
            if(j == 4)
                System.out.println();
        }
    System.out.println("Bingo status: " + bingo);
   }
}

这是Token类:

import java.util.ArrayList;
import java.util.Random;
public class Token
{
    ArrayList<Integer> number;
    public Token()
    {
        number = new ArrayList<Integer>();
        for(int i = 0; i < 75; i++)
            number.add((int)(Math.random() * 75 + 1));
    }
    public int getNumber()
    {
        Random rand = new Random();
        int temp = number.get(rand.nextInt(number.size()));

        number.remove(temp);
        return temp;
    }

    public int getLetter()
    {
        Random rand = new Random();
        int temp = number.get(rand.nextInt(number.size()));
        if(temp >= 1 && temp <= 15)
            return 0; //B
        else if(temp >= 16 && temp <= 30)
            return 1; //I
        else if(temp >= 31 && temp <= 45)
            return 2; //N
        else if(temp >=46 && temp <= 60)
            return 3; //G
        return 4; //O
    }
}

谢谢大家!我知道......这是一个很长的计划。

2 个答案:

答案 0 :(得分:1)

您正在调用player.Mark(tempNumber,tempLetter);,其中playerArrayList<Board>,但没有该方法。它在类Board本身中定义。

因此,您希望get此列表中的当前Board个实例,并在 上致电Mark

即。 win = player.get(currentPlayerIndex).Mark(tempNumber,tempLetter);

根据您的逻辑,您需要一些获得当前播放器/电路板的currentPlayerIndex

注意:如@opensam所述,您需要将while(win = false)替换为while(win == false),否则您将false分配给win并且在你可以进行一次迭代之前,循环就会中断。

答案 1 :(得分:0)

TL; DR

  

并且Mark()出现了错误。

&#39;播放&#39;是ArrayList类的实例,而Mark(int,int)是为类Board定义的方法。如果要调用方法Mark(int,int),则必须通过类Board的实例调用它。如果您不明白这意味着什么,请浏览thisthis