好的,程序的工作原理如下: 通过运行该程序,它将要求用户输入将玩乐透的玩家数量。例如,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/