Hey guys.
I just had a problem with converting, and you guys helped me fixed it. So, here is my finished code:
import java.util.Scanner; public class RandomKeyCode { public static void generate() { double c1, c2, c3, c4, c5; String ccc1, ccc2, ccc3, ccc4, ccc5; String[] choice = new String[5]; String keycode; c1 = ((Math.random()*46)+1); ccc1 = find(c1); c2 = ((Math.random()*46)+1); ccc2 = find(c2); c3 = ((Math.random()*46)+1); ccc3 = find(c3); c4 = ((Math.random()*46)+1); ccc4 = find(c4); c5 = ((Math.random()*46)+1); ccc5 = find(c5); choice[0] = ccc1; choice[1] = ccc2; choice[2] = ccc3; choice[3] = ccc4; choice[4] = ccc5; keycode = choice[0] + choice[1] + choice[2] + choice[3] + choice[4]; System.out.print("Your keycode is: " + keycode + "."); System.out.print("Your numbers were " + c1 + " " + c2 + " " + c3 + " " + c4 + " " + c5); } public static void enter() { } public static String find(double c) { String out = null; switch((byte)c) { case 1: out = "a"; case 2: out = "b"; case 3: out = "c"; case 4: out = "d"; case 5: out = "e"; case 6: out = "f"; case 7: out = "g"; case 8: out = "h"; case 9: out = "i"; case 10: out = "j"; case 11: out = "k"; case 12: out = "l"; case 13: out = "m"; case 14: out = "n"; case 15: out = "o"; case 16: out = "p"; case 17: out = "q"; case 18: out = "r"; case 19: out = "s"; case 20: out = "t"; case 21: out = "u"; case 22: out = "v"; case 23: out = "w"; case 24: out = "x"; case 25: out = "y"; case 26: out = "z"; case 27: out = "1"; case 28: out = "2"; case 29: out = "3"; case 30: out = "4"; case 31: out = "5"; case 32: out = "6"; case 33: out = "7"; case 34: out = "8"; case 35: out = "9"; case 36: out = "0"; case 37: out = "!"; case 38: out = "@"; case 39: out = "#"; case 40: out = "$"; case 41: out = "%"; case 42: out = "^"; case 43: out = "&"; case 44: out = "*"; case 45: out = "("; case 46: out = ")"; } return out; } public static void main(String[] args) { String q = null; //question variable Scanner scan = new Scanner(System.in); System.out.print("Do you want to generate your key code, or do you want to enter it?"); q = scan.nextLine(); switch(q.toLowerCase()) { case("generate") : generate(); case("enter") : enter(); } } }
So, I try to do it and here is the output:
Do you want to generate your key code, or do you want to enter it?generate
Your keycode is: ))))).Your numbers were 2.2230319103237237 40.409992358385196 46.01823508515554 8.75421673376253 7.440266533810639
As you can see, it gives me random numbers, but since the switch can't handle the output of the decimal numbers, the switch defaults to the last character.
I need to know how to make the switch able to sense the difference or how to switch the type so that it won't give me decimals.
Help?
-Silent