mdavda2013
Nov 5, 2013, 07:44 PM
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..
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..