java - 比较2个二维数组JAVA

标签 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 名以上的玩家。

更新我自己回答了。

最佳答案

首先排序中奖乐透

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

关于java - 比较2个二维数组JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323424/

相关文章:

java - 如何检查 String[] 类型的页面属性?

java - 将 bundle Assets 的文件路径传递到 Android 上的外部库

javascript - JSP Javascript,更新java对象值

c++ - 重载索引运算符以模仿 POD 多维数组?

python - 如何删除 numpy 数组中所有 numpy 数组中的第 n 个元素?

c - 如何将二维数组的每个元素递增 2

java - 如何知道返回的是哪种类型的 HttpEntity

java - 删除最后一个逗号java

javascript - 使用数组中的值填充下拉列表框

javascript - 从二维数组中删除多个数组