简体   繁体   中英

Java lottery repeat program

I need this lottery program to repeat itself a certain number of times and then stop, but I am having trouble on where to place the for loop (if that's what I should use). How can I display the percentage of times 0, 1, 2, 3, 4 or 5 numbers matched out of all the runs?

    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

As for the loop: It should go in main around the things you're doing for 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();
    }
}

As for counting the percentages you'll need to modify the code for 'write_data()' to store or return the number of matches (probably in an array), then divide by the total number of runs for each.

要弄清楚这些数字相对于其余数字有多少,请为每个数字保留一个计数器,为总数保留一个,然后进行一些简单的除法。

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

This accomplishes all of the requirements you have mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM