简体   繁体   中英

Comparing 2 two-dimensional arrays JAVA

Okay, here's how the program should work: By running the program, it will ask the user to input number of players that will play the Lotto. For example, 3. So 3 rows of array would be made and 6 column which is default column. Now after generating random digits in Lotto array. It will then generate a winning Lotto.

The problem is here: Ive done the part of generating the player's lotto and the winning lotto. However, I cant compare winning lotto to players lotto/s. For example:

Player 0 : 1 15 30 41 56 12

Player 1: 31 65 78 43 29 8

Player 2: 41 28 1 6 38 14

Winning Lotto: 15 30 56 12 41 1

The winner should be the Player 0 even the sorting is not exact. But the problem is I cant compare the two dimensional arrays.

Here's the code:

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

I dont have a lot of knowledge about java since Im still a 1st year college. So can I ask simple codes? I want to do the comparing in for loops. I only know basic. I hope the most simple code can do.

UPDATED ADDED 1 METHOD FOR CHECKING IT WORKS FOR 1 PLAYER BUT DOESNT IN MORE THAN 1.

UPDATE answered it myself.

First sort winning lotto

Arrays.sort(winninglotto);

then for each of your players get the numbers array sort it and then see if they are equal to winning lotto

Arrays.sort(playernumbers);
return Arrays.equals(playernumbers, winninglotto);

like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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