Ask Experts Questions for FREE Help !
Ask
    mdavda2013's Avatar
    mdavda2013 Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 5, 2013, 07:44 PM
    Lottery in java
    We were given fun project to do, The Question to solve was the following

    Each of the participating students will first be assigned a lowercase english letter (a, b, c, etc.). The lottery is then performed in two rounds. In the first round, you provide the list of players to the game. These players must be lower case and unique. The next round is the set of losers, or characters within the player's list. Finally the winners are chosen which are the players that are not in the loser set. If the user decides not to enter a list of winners, than he or she must generate the list of losers with a specific algorithm specified below.

    So I came up with the following code but something is wrong "I am not getting results i wanted" more at the end of the question, Can someone help me?

    import java.util.Scanner;
    import java.util.Random;

    public class Lottery{
    public static void main(String[] args){
    Scanner kb = new Scanner(System.in);
    long seed = Long.parseLong(args[0]);
    Random rand = new Random(seed);
    System.out.println(rand);
    String loserList = "";

    System.out.println("Enter players set: ");
    String players = kb.next();
    String winnersList = players;
    System.out.println("Enter loser set: ");

    for (int i=0; i<5; i++){

    if(winnersList.length()==1){
    loserList = loserList + winnersList;
    }

    int randomChar = winnersList.indexOf(rand.nextInt());
    //winnersList(Random.nextInt(winnersList.length()));
    int randomIndex = winnersList.indexOf(randomChar);
    loserList = loserList + randomIndex;

    }

    System.out.println("Randomly generated string is: " + loserList);
    System.out.println("Winners are: " + winnersList);
    }
    }
    Error description:

    The Results were --
    Enter players set: xcfgbam
    Enter loser set:
    Randomly generated string is: -1-1-1-1-1
    Winners are: xcfgbam
    I am not able to enter the loser string not am i being able to create proper random string, i am getting "-1... " for any test subjects..
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #2

    Nov 30, 2013, 05:21 AM
    One tactic to expose problems as you are coding and then find where things go awry is to intersperse println statements periodically to check the value of variables after they are assigned. These statements can be commented out in the final code. Obviously, -1 is bogus, but loserlist was assigned an empty string at the beginning, so how and where could loserlist have gotten a bogus value?

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java netbeans program- Connection between java and mysql,How to conn [ 0 Answers ]

which I wrote from a book but when I run it, it shows exception please give me the code to connect jlistbox to database when I click on btn1. btn coding should search "name" in database and shows all the names in listbox my coding: here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...

Lottery [ 15 Answers ]

What is the best way to forecat lottery numbers ? I tried with tarot but it didn` worked . Any tip ?

DV lottery [ 0 Answers ]

I am a 3rd year MBBS student of Bangladesh.it needs 3 years more to complete the course here.But I have won DV lottery 2011.Is it possible to go to USA after completing my course by my current DV?

Lottery [ 33 Answers ]

Alty the Great... What are the lottery numbers for Saturday? PowerBall? Will I ever get to go Paris with my husband?

National lottery [ 3 Answers ]

Is uacl universal air courier service a real business in the uk. And allso is uk national lottery international real.


View more questions Search