gpt4 book ai didi

Java 彩票重复程序

转载 作者:行者123 更新时间:2023-12-01 11:49:09 24 4
gpt4 key购买 nike

我需要这个彩票程序重复一定次数然后停止,但我在放置 for 循环的位置上遇到了麻烦(如果这就是我应该使用的)。如何显示 0、1、2、3、4 或 5 个数字在所有运行中匹配的次数百分比?

    package assignment5;
import javax.swing.JOptionPane;

import java.util.Random;
public class assignment5
{
public static void main(String[] args)
{
lottery pic=new lottery();
pic.Get_player_numbers();
pic.Get_jackpot_number();
pic.Check_winner ();
pic.Write_data();

}
}
class lottery
{

int[] picks= new int[5];
int[] cpick=new int[5];
int i;
int j,c;
int match=0;

void Get_player_numbers ()

{
int ctemp,cdupflag=0;
for(j=0;j<=4;++j)
{
//YOU DO NOT NEED THE CNUMBERFLAG
//IF YOU GENERATED THE NUMBERS CORRECLTY, THE COMPUTER WILL NOT GENERATE ONE ABOVE 99 OR LESS THAN 1
cdupflag=0;
while(cdupflag==0)
{
ctemp = (int)Math.round(Math.random()*99)+1;
cdupflag=1;
for(c=0;c<=j;++c)
{
if(ctemp==cpick[c])
{
cdupflag=0;
}
}//inner for loop
if(cdupflag==1)
cpick[j]=ctemp;
}
}
String Jackpot="User Lottery numbers are: "+"\n";
//String computer = "";
for(j=0;j<=4;++j)
{
if(j==4)
Jackpot=Jackpot+cpick[j];
else
Jackpot=Jackpot+cpick[j]+"-";
}

JOptionPane.showMessageDialog(null,Jackpot,"Output:",JOptionPane.INFORMATION_MESSAGE);

}

//void jackpot()
void Get_jackpot_number()
{

int ctemp,cdupflag=0;
for(j=0;j<=4;++j)
{
//YOU DO NOT NEED THE CNUMBERFLAG
//IF YOU GENERATED THE NUMBERS CORRECLTY, THE COMPUTER WILL NOT GENERATE ONE ABOVE 99 OR LESS THAN 1
cdupflag=0;
while(cdupflag==0)
{
ctemp = (int)Math.round(Math.random()*99)+1;
cdupflag=1;
for(c=0;c<=j;++c)
{
if(ctemp==cpick[c])
{
cdupflag=0;
}
}//inner for loop
if(cdupflag==1)
cpick[j]=ctemp;
}
}
String Jackpot="Computer Lottery numbers are: "+"\n";
//String computer = "";
for(j=0;j<=4;++j)
{
if(j==4)
Jackpot=Jackpot+cpick[j];
else
Jackpot=Jackpot+cpick[j]+"-";
}

JOptionPane.showMessageDialog(null,Jackpot,"Output:",JOptionPane.INFORMATION_MESSAGE);

}


void Check_winner ()
{

for(int i=0;i<=4;++i)
{
for(int j=0;j<=4;++j)
{
if(picks[i]==cpick[j])
{
match=match+1;
}
}
}

}


void Write_data ()
{
String print = "";

if(match==0)
{
print=print+"There is no match"+"\n";
print=print+"please try again "+"\n";
}
else
if(match==1)
{
print=print+"There is one match"+"\n";
print=print+"You won 100 Dollars "+"\n";
}
else
if(match==2)
{
print=print+"There are two matches"+"\n";
print=print+"You won 1,000 Dollars"+"\n";
}
else
if(match==3)
{
print=print+"There are three matches"+"\n";
print=print+"You won 10,000 Dollars "+"\n";
}
else
if(match==4)
{
print=print+"There are four matches"+"\n";
print=print+"You won 100,000 Dollars "+"\n";
}
else
if(match==5)
{
print=print+"There are five matches"+"\n";
print=print+"You won 1,000,000 Dollars"+"\n";
}
JOptionPane.showMessageDialog(null,print,"Output:",JOptionPane.INFORMATION_MESSAGE);
}}

//end of class lottery

最佳答案

至于循环:它应该主要围绕您为彩票所做的事情

public static void main(String[] args)
{
for(int i = 0; i < [NumberOfRepeats]; i++)
{
lottery pic=new lottery();
pic.Get_player_numbers();
pic.Get_jackpot_number();
pic.Check_winner ();
pic.Write_data();
}
}

至于计算百分比,您需要修改“write_data()”的代码来存储或返回匹配数(可能在数组中),然后除以每个匹配的运行总数。

关于Java 彩票重复程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28919381/

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