比较2个二维数组JAVA

时间:2014-01-24 02:40:08

标签: java arrays multidimensional-array compare

好的,这是程序应该如何工作: 通过运行程序,它将要求用户输入将播放乐透的玩家数量。例如,3。将生成3行数组,6列为默认列。现在在Lotto数组中生成随机数字。然后它将生成一个获胜的乐透。

问题在于: 我已经完成了生成玩家乐透和获胜乐透的部分。但是,我无法将获胜的乐透与玩家乐透相提并论。例如:

球员0:1 15 30 41 56 12

球员1:31 65 78 43 29 8

球员2:41 28 1 6 38 14

获胜乐透:15 30 56 12 41 1

获胜者应该是玩家0,即使排序不准确。 但问题是我无法比较二维数组。

以下是代码:

import javax.swing.*; import java.util.*; public class test{ public static void main(String asd[]){ int players = Integer.parseInt(JOptionPane.showInputDialog("Any NUMBER of PLAYERS\n[0] to exit")); while(players != 0) { int typeOfLotto = Integer.parseInt(JOptionPane.showInputDialog("Type:\n[42] - SixFortyTwo\n[45] - SixFortyFive\n" + "[49] - SixFortyNine\n[55] - SixFiftyFive")); String output="", playersLotto="", winning=""; int taya = 6; int index; int[][] Lotto = new int[players][taya]; int[][] winningLotto = new int[1][taya]; for(index = 0; index<players; index++) { for(int column = 0; column<6; column++) { Lotto[index][column] = numberGen(typeOfLotto,Lotto,index); output += Lotto[index][column] + " "; } playersLotto += "Player " + index + ": " + output +"\n"; output=""; } //winning lotto for(int column = 0; column<6; column++) { winningLotto[0][column] = winningNumberGen(typeOfLotto,winningLotto); winning += winningLotto[0][column] + " "; if(column==5) winning = "Winning lotto: " + winning; } String win = checking(Lotto,winningLotto,players); JOptionPane.showMessageDialog(null, playersLotto + "\n" + winning + "\n" + win); players = Integer.parseInt(JOptionPane.showInputDialog("Any NUMBER of PLAYERS\n[0] to exit")); } } public static int numberGen(int typeOfLotto,int Lotto[][],int index) { int random=0, loop=0; if(typeOfLotto == 42) { random = (int)(1+Math.random()*42); for(loop=0; loop<6; loop++) { if(Lotto[index][loop] == random) return numberGen(typeOfLotto,Lotto,index); } } return random; } //winning public static int winningNumberGen(int typeOfLotto,int winningLotto[][]) { int random=0, loop=0; if(typeOfLotto == 42) { random = (int)(1+Math.random()*42); for(loop=0; loop<6; loop++) { if(winningLotto[0][loop] == random) return winningNumberGen(typeOfLotto,winningLotto); } } return random; } public static String checking(int Lotto[][], int winningLotto[][], int players) { int win[] = new int[6]; int panalo=0; String winner="", output=""; for(int index=0; index<players;index++) { for(int loop=0;loop<6;loop++) { if(winningLotto[0][0] == Lotto[index][loop]) win[loop] = Lotto[index][loop]; if(winningLotto[0][1] == Lotto[index][loop]) win[loop] = Lotto[index][loop]; if(winningLotto[0][2] == Lotto[index][loop]) win[loop] = Lotto[index][loop]; if(winningLotto[0][3] == Lotto[index][loop]) win[loop] = Lotto[index][loop]; if(winningLotto[0][4] == Lotto[index][loop]) win[loop] = Lotto[index][loop]; if(winningLotto[0][5] == Lotto[index][loop]) win[loop] = Lotto[index][loop]; } } if(win[0] > 0) panalo++; if(win[1] > 0) panalo++; if(win[2] > 0) panalo++; if(win[3] > 0) panalo++; if(win[4] > 0) panalo++; if(win[5] > 0) panalo++; if(panalo > 3) { for(int loop=0;loop<6;loop++) { winner += win[loop] + " "; } output = "Winner: " + winner; } else if(panalo < 3) output = "no winner"; return output; } }

我还没有很多关于java的知识,因为我还是大学一年级。我可以问简单的代码吗?我想在for循环中进行比较。我只懂基本的。我希望最简单的代码可以做到。

更新增加了1个方法,用于检查1个播放器的工作但不超过1个。

更新自己回答。

1 个答案:

答案 0 :(得分:1)

首先获胜的乐透

Arrays.sort(winninglotto);

然后为每个玩家获取数字排序,然后查看它们是否等于赢得乐透

Arrays.sort(playernumbers);
return Arrays.equals(playernumbers, winninglotto);
像这样:

public static String checking(int Lotto[][], int winningLotto[][], int players)
{
 Arrays.sort(winningLotto[0]);
 for(int i =0; i < players;i++){
     Arrays.sort(Lotto[i]);
    if(Arrays.equals(Lotto[i], winningLotto[0])){
       //we have a winnder
    }
 }
}