2014-01-24 5 views
0

좋아, 다음은 프로그램 작동 방식입니다. 프로그램을 실행하면 사용자에게 로또를 재생할 플레이어 수를 입력하라는 메시지가 표시됩니다. 예를 들어, 3입니다. 그래서 3 행의 배열이 만들어지고 6 열은 기본 열입니다. 이제 Lotto 배열에서 임의의 숫자를 생성합니다. 그러면 Lotto가 생성됩니다.2 차원 배열 2 개 비교 JAVA

문제는 여기에 있습니다. 필자의 로또와 승리하는 로또를 생성하는 부분을 수행했습니다. 그러나, 나는 로또를 우승자/로또와 비교합니다.

플레이어 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이어야합니다. 하지만 문제는 2 차원 어레이를 비교할 수 없다는 것입니다.

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

내가 메신저 아직도 1 년제 대학 이후 자바에 대한 지식을 많이 가지고 있겠지 :

여기에 코드입니다. 그래서 간단한 코드를 물어볼 수 있습니까? for 루프를 비교하고 싶습니다. 나는 단지 기본적인 것을 알고있다. 가장 간단한 코드가 할 수 있기를 바랍니다.

업데이트]는 IT가 1보다

UPDATE는 그것을 자신을 대답 1 일 플레이어하지만 나던 작동 확인을위한 한 방법을 추가했습니다.

답변

1

첫 번째 종류의 경력 로또하여 플레이어의 각 숫자 배열을 얻기 위해 그들이이 같은 로또

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

승리에 동일한 경우

Arrays.sort(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 
    } 
} 
} 
+0

메인에 적용할까요? 아니면 새로운 방법을 만들겠습니까? – lazygeniusLANZ

+0

그리고 어떻게 로또 배열의 로또 번호를 정렬합니다. – lazygeniusLANZ