尝试传递2D数组时出现不兼容的类型错误

时间:2019-11-08 09:20:04

标签: java arrays methods compiler-errors

我正在尝试将2d数组传递给另一个方法,但是它一直给我错误“无法将int转换为int [] []”,而对于返回行则相反。当我从Deal方法中删除[] []时,它给了我错误,但是反过来说“ int [] []无法转换为int”。我在这里做什么错了?

//2-6 players face eachother in a game of Go Fish!

import java.util.Scanner;
import java.lang.String;
import java.util.Random; //RNG

public class GoFish{
public static void main(String []args){
   Scanner in = new Scanner(System.in);
   System.out.println("This is a game simulation of Go Fish! It supports up to 6 players.");
   int Players = 0;
   System.out.println("How many players will be playing?");
   Players = in.nextInt();          //# of players
   while(Players < 2 || Players > 6){
      System.out.println("Please enter a number of players, between 2 and 6");
      Players = in.nextInt();
   }
   int [][] Cards = new int[Players + 1][54]; //Array for the cards. Final entry is the number of cards within the hand/deck, second to last number is the number of points.
   String [] Deck = new String [52];//This Array will contain the name for each card.
   Cards [0][52] = 52;              //Array 0 is the deck, Array 1 is the hand for player 1, Array 2 is the hand for player 2, ect.
   for(int i = 0; i < 52; i++){     //Fill the deck with cards.
      Cards [0][i] = 1;
   }
   int n = 5;                       //Number of cards to deal to the users.
   if(Players == 2)
      n = 7;                        //Increases to 7 if only 2 players are playing.
   for(int i = 0; i < n; i++){      //Deal cards for both players.
      for(int j = 0; j < Players; j++){
         Cards = Deal(Cards, Deck, Players);
      }
   }
}

static int Deal(int Cards[][], String Deck[], int Players){
return Cards;
}
}

1 个答案:

答案 0 :(得分:0)

函数Deal返回 int ,但您将Cards定义为: int [] [] Cards。