Monday, July 27, 2009

How can i write a java program that will generate 4 DIFFERENT random letters from the letters ?

LETTERS FROM A-F


thats my answer but it does not work


String zero = "A";


String one = "B";


String two = "C";


String three = "D";


String four = "E";


String five = "F";





Random generator = new Random();


int random1, random2, random3, random4;





random1 = generator.nextInt(5);


do {


random2 = generator.nextInt(5);


}while(random2 == random1);


do {


random3 = generator.nextInt(5);


}while(random3 == random1 || random3 == random2);


do {


random4 = generator.nextInt(5);


}while(random4 == random2 || random4 == random2 || random4 == random3);





String randomLetter1, randomLetter2, randomLetter3, randomLetter4;





switch(random1)


{


case 0:


randomLetter1 = zero;


break;


case 1:


randomLetter1 = one;


break;


case 2:


randomLetter1 = two;


break;


case 3:


randomLetter1 = three;


break;


case 4:


randomLetter1 = four;


break;


case 5:


randomLetter1 = five;


break;


}





switch(random2)


{


case 0:


randomLetter2 = zero;


break;


case 1:


randomLetter2 = one;


break;


case 2:


randomLetter2 = two;


break;


case 3:


randomLetter2 = three;


break;


case 4:


randomLetter2 = four;


break;


case 5:


randomLetter2 = five;


break;


}


switch(random3)


{


case 0:


randomLetter3 = zero;


break;


case 1:


randomLetter3 = one;


break;


case 2:


randomLetter3 = two;


break;


case 3:


randomLetter3 = three;


break;


case 4:


randomLetter3 = four;


break;


case 5:


randomLetter3 = five;


break;


}


switch(random4)


{


case 0:


randomLetter4 = zero;


break;


case 1:


randomLetter4 = one;


break;


case 2:


randomLetter4 = two;


break;


case 3:


randomLetter4 = three;


break;


case 4:


randomLetter4 = four;


break;


case 5:


randomLetter4 = five;


break;


}

How can i write a java program that will generate 4 DIFFERENT random letters from the letters ?
You have 6 slots, not five. The old, dice throw is:





int throw = generator.nextInt(6) + 1;





the range gives never a 6, without the + 1 you can get a 0, and that is not on the dice.





All I can tell you is you have System.out.println of the random value and evaluate your math. If it were me, this problem's solution is a Collections implementation.





Random, for me anyway, is for animating snowflakes or asteroids. Your problem is more like a card game.


No comments:

Post a Comment