gpt4 book ai didi

java - 显示彩票中奖者

转载 作者:行者123 更新时间:2023-12-01 13:03:44 24 4
gpt4 key购买 nike

我创建了一个彩票程序,随机生成 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.");

关于java - 显示彩票中奖者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23373756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com