Making setting variables on userinput more efficient.
that's a weird situation to work with
anyway, it's not really a performance problem, but maybe more of a readibility problem
you could remove the longest case sequence
case 1: case 3: case 4: case 6: case 8: case 9: case 11: case 13: case 17: case 18: case 24: kantone_preis = 0.1997; break;
and replace it with a
default: kantone_preis = 0.1997;
but that would just make the code shorter and worse
________________________________________________________________________________
other method: you could go bananas and decide that your key numbers are bits, so you have a 32 bits integer and
case 5: case 12: case 14: case 22:
means that the 5th, 12th, 14th and 22nd bits are 1
so you have
00000000010000000101000000100000bin = 4214816dec
if i didn't fuck up writing the binary thing (note: you start counting from the 0th bit)
now you have a mathematical bond between your numbers and the action that you need to perform
if( Math.pow(2, kantone_spin_pos) & 4214816 != 0) kantone_preis = 0.2197;
or, with more elegance
int userChoice = Math.pow(2, kantone_spin_pos);int group1 = 4214816;if( userChoice & group1 != 0) // if the user choice belongs to the first group of numbers kantone_preis = 0.2197;
i'll leave here the results for the other cases as well, just in case you need them
[2, 7, 15, 16, 19, 20, 23] = 10059908
[3, 4, 6, 8, 9, 11, 13, 17, 18, 24] = 17181528
[5, 12, 14, 22] = 4214816
[10, 21] = 2098176
i don't have a java compiler now, i can't be sure that the code is 100% working
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now