我需要我的彩票计划来计算10次运行后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)
{
for(int i = 0; i <10; i++)
{
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 = "";
int jackpot = 2500000;
int counter0 = 0;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
if(match==0)
{
counter0++;
print=print+"There is no match"+"\n";
print=print+"please try again "+"\n";
jackpot=jackpot+25000;
}
else
if(match==1)
{
counter1++;
print=print+"There is one match"+"\n";
print=print+"You won 100 Dollars "+"\n";
jackpot=jackpot+100000;
}
else
if(match==2)
{
counter2++;
print=print+"There are two matches"+"\n";
print=print+"You won 1,000 Dollars"+"\n";
jackpot=jackpot+250000;
}
else
if(match==3)
{
counter3++;
print=print+"There are three matches"+"\n";
print=print+"You won 10,000 Dollars "+"\n";
jackpot=jackpot+500000;
}
else
if(match==4)
{
counter4++;
print=print+"There are four matches"+"\n";
print=print+"You won 100,000 Dollars "+"\n";
jackpot=jackpot+1000000;
}
else
if(match==5)
{
counter5++;
print=print+"There are five matches"+"\n";
print=print+"You won "+jackpot+" Dollars"+"\n";
jackpot=2500000;
}
JOptionPane.showMessageDialog(null,print,"Output:",JOptionPane.INFORMATION_MESSAGE);
System.out.println("zero matchers occured " +counter0+ " times");
System.out.println("The current jackpot is " +jackpot+ "");
}
}
//end of class lottery
答案 0 :(得分:0)
首先,由于您的代码有点混乱且没有注释,我不确定理解所有内容,您应该尝试清除它,您将更容易理解并发现错误。
关于我所看到的问题:
如果我正确理解你想要保留&#34; Jackpot&#34;超过10轮。这不会发生,因为在您正在执行的主要功能中
for(int i = 0; i <10; i++)
{
lottery pic = new lottery();
....
}
因此,您正在为每次运行创建一个新的彩票,并在每次运行时用空的替换以前的彩票。也许这会起作用:
lottery pic = new lottery();
for(int i = 0; i <10; i++)
{
....
}