我创建了一个彩票程序,在0-9之间随机生成3个数字,然后随机生成3个中奖数字。我需要有关如何使程序显示中奖者(如果有)和显示中奖人数的帮助。

所以像:
获奖者:

人1

人5

获奖人数:2

这是我的程序

import java.util.Random;

public class TwoDArray
{
public static void main(String[] args)
{
    int[][] table = new int[50][3];
    int[][] win = new int[1][3];
    Random rand = new Random();
    int i = 1;

    // Load the table with values
    for (int row=0; row < table.length; row++)
        for (int col=0; col < table[row].length; col++)
            table[row][col] = rand.nextInt(7-0 +1)+0 + col;

    // Load the winning Values
    for (int row=0; row < win.length; row++)
        for(int col=0; col < win[row].length; col++)
            win[row][col] = rand.nextInt(7-0 +1)+0 + col;

    // Print the table of People
    for (int row=0; row < table.length; row++)
    {
        System.out.print("Person" + i++ +":\t");
            for (int col=0; col < table[row].length; col++)
                System.out.print(table[row][col] + "\t");
                System.out.println();
    }

    //Print the Winning Numbers
    for (int row=0; row < win.length; row++)
    {

        System.out.print("\nThe winning numbers are:\t");
            for(int col=0; col < win[row].length; col++)
                System.out.print(win[row][col] + "\t");
                System.out.println();
    }


}
}

最佳答案

您想要另一个for循环。就像是:

 int counter = 0;
 for (int i =0; i < table.length; i++){
     if (table[i][0] == win[0][0] && table[i][1] == win[0][1] && table[i][2] == win[0][2])
     {
          counter++;
          System.out.println("Person " + i);
     }
 }

 System.out.println("There were " + counter + " winners.");

07-26 00:50