简体   繁体   English

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

[英]How to use ArrayList to call a method from another class in Java?

I am a total beginner at Java and we have a program to do on Bingo.我是 Java 的初学者,我们有一个关于 Bingo 的程序要做。 You have to let the user enter the number of players and each player receives a board.您必须让用户输入玩家数量,然后每个玩家都会收到一个棋盘。 Then a random token is called from another class and you have to use that token to mark it on the board.然后从另一个类调用一个随机令牌,您必须使用该令牌在板上标记它。 I don't know how to figure out who won...and there's an error on Mark().我不知道如何确定谁赢了……而且 Mark() 上有错误。 Could anyone help?有人可以帮忙吗? Thanks.谢谢。

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);
     }
  }
}

Here is the Board class:这是 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);
   }
}

And here is the Token class:这是 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
    }
}

Thank you everyone!谢谢大家! I know...this is a long program.我知道……这是一个很长的程序。

You're calling player.Mark(tempNumber,tempLetter);你打电话给player.Mark(tempNumber,tempLetter); where player is an ArrayList<Board> which doesn't have that method.其中player是一个没有该方法的ArrayList<Board> It is defined in the class Board itself.它是在类Board本身中定义的。

So you want to get the current Board instance from this list and call Mark on that .所以你想从这个列表中get当前的Board实例并在那个上调用Mark

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

Depending on your logic you need some currentPlayerIndex that gets the current Player/Board.根据您的逻辑,您需要一些获取当前玩家/棋盘的currentPlayerIndex

Note: as @opensam mentioned, you need to replace while(win = false) with while(win == false) or else you'll assign false to win and the loop breaks before you can do one iteration.注意:正如@opensam 提到的,你需要用while(win == false)替换while(win = false)否则你将false分配给win并且循环在你可以进行一次迭代之前中断。

TL;DR TL; 博士

and there's an error on Mark().并且 Mark() 上有错误。

'player' is an instance of ArrayList class whereas Mark(int,int) is a method defined for your class Board. 'player' 是 ArrayList 类的实例,而 Mark(int,int) 是为您的类 Board 定义的方法。 If you want to call method Mark(int,int), you will have to call it via an instance of class Board.如果要调用方法 Mark(int,int),则必须通过类 Board 的实例调用它。 If you dont understand what that means then please go through this or this .如果你不明白这意味着什么,那么请通过这个这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM