我需要这个抽奖程序重复一定次数然后停止,但是我在找到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
答案 0 :(得分:1)
关于循环:它应该主要围绕你为彩票做的事情
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();
}
}
至于计算您需要修改&#39; write_data()&#39;的代码的百分比。存储或返回匹配的数量(可能在数组中),然后除以每个匹配的总运行次数。
答案 1 :(得分:0)
要弄清楚这些数字与其余数字相关的数量,保留每个数字的计数器和总数量的一个,然后进行一些简单的划分。
答案 2 :(得分:0)
package loop;
//import javax.swing.JOptionPane;
import java.io.*;
public class loop
{
public static void main(String[] args)
{
Lottery pic=new Lottery();
for(int i = 0; i < 500; i++) {
pic.getPlayerNumbers();
pic.getJackpotNumbers();
pic.checkWinner();
pic.assignPayout();
pic.stringBuilderOne();
}
pic.stringBuilderTwo();
pic.writeData();
}
}//end loop
class Lottery
{
private final int[] ppick= new int[5];
private final int[] cpick=new int[5];
private int match=0;
private int i;
private double matchcount0=0;
private double matchcount1=0;
private double matchcount2=0;
private double matchcount3=0;
private double matchcount4=0;
private double matchcount5=0;
private int jackpot = 25000000;
private int payout;
private final StringBuilder builderOne = new StringBuilder();
private final StringBuilder builderTwo = new StringBuilder();
void getPlayerNumbers ()
{
int ptemp,pdupflag;
for(i =0; i <=4;++i)
{
pdupflag=0;
while(pdupflag==0)
{
ptemp = (int)Math.round(Math.random()*99)+1;
pdupflag=1;
int p;
for(p =0; p<= i;++p)
{
if(ptemp==ppick[p])
{
pdupflag=0;
}
}//inner for loop
if(pdupflag==1)
ppick[i]=ptemp;
}
}
}//end getPlayerNumbers
void getJackpotNumbers()
{
int ctemp,cdupflag;
int j;
for(j =0; j <=4;++j)
{
cdupflag=0;
while(cdupflag==0)
{
ctemp = (int)Math.round(Math.random()*99)+1;
cdupflag=1;
int c;
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]+"-";
}
}//end getJackpot numbers
void checkWinner ()
{
match=0;
for(int i=0;i<=4;++i)
{
for(int j=0;j<=4;++j)
{
if(ppick[i]==cpick[j])
{
match=match+1;
}
}
}
}//end checkWinner
void assignPayout () {
if (match == 0) {
matchcount0 = matchcount0 + 1;
payout = 0;
jackpot = jackpot + 25000;
} else if (match == 1) {
matchcount1 = matchcount1 + 1;
payout = 100;
jackpot = jackpot + 100000;
} else if (match == 2) {
matchcount2 = matchcount2 + 1;
jackpot = jackpot + 250000;
payout = 1000;
} else if (match == 3) {
matchcount3 = matchcount3 + 1;
jackpot = jackpot + 500000;
payout = 10000;
} else if (match == 4) {
matchcount4 = matchcount4 + 1;
jackpot = jackpot + 1000000;
payout = 100000;
} else if (match == 5) {
matchcount5 = matchcount5 + 1;
payout = jackpot;
jackpot = 2500000;
}
}//end assignPayout
void stringBuilderOne ()
{
builderOne.append(System.getProperty("line.separator"))
.append("Current Jackpot Player# Winner# #Matched Payout\n")
.append(jackpot).append(" ")
.append(ppick[0]).append(" ")
.append(ppick[1]).append(" ")
.append(ppick[2]).append(" ")
.append(ppick[3]).append(" ")
.append(ppick[4]).append(" ")
.append(cpick[0]).append(" ")
.append(cpick[1]).append(" ")
.append(cpick[2]).append(" ")
.append(cpick[3]).append(" ")
.append(cpick[4]).append(" ")
.append(match).append(" ")
.append(payout);
}//end stringBuilderOne
void stringBuilderTwo ()
{
builderTwo.append(System.getProperty("line.separator"))
.append("\nThe percent of plays where 0 numbers matched = ")
.append(matchcount0 / i * 100).append("%\nThe percent of plays where 1 numbers matched = ")
.append(matchcount1 / 10).append("%\nThe percent of plays where 2 numbers matched = ")
.append(matchcount2 / 10).append("%\nThe percent of plays where 3 numbers matched = ")
.append(matchcount3 / 10).append("%\nThe percent of plays where 4 numbers matched = ")
.append(matchcount4 / 10).append("%\nThe percent of plays where 5 numbers matched = ")
.append(matchcount5 / 10).append("%\n");
}//End stringBuilderTwo
void writeData ()
{
try
{
BufferedWriter lotteryOutput = new BufferedWriter(new FileWriter("output.dat"));
lotteryOutput.write(builderOne.toString());
lotteryOutput.write(builderTwo.toString());
lotteryOutput.close();
}//end try
catch (IOException error)
{
//there was an error on the write to the file
System.out.println("Error on file write " + error);
}//end error
}//end writeData
}//end of class lottery
这完成了您提到的所有要求。