Don't use modulo, to wrap, find the midpoint of what interval you are trying to wrap and make 2 cases for it, if it is above that midpoint, subtract whatever number you have from your interval. in my case my interval was the alphabet, and i had to do 26 - x.
Also, to the OP. Just modify my algorithm to get what you want. Also for the 3 decimal value, try using the "printf" function. Look it up on the java API if you're unsure of how to use it.
Whats wrong with closing the scanner at the end of your program ?
And whats wrong with doing the calculations in the while loop? how else can it update the distance?
I know that this can be done with a bunch of if else and even more nested if else statements, although editing that code will be chaos, I was told it could be done using modulus and I am trying to keep it simple.
I am writing a program which basically creates a 3 letter lock, say CAT for example, then the lock can be opened, closed, reset, and letters can be set individually. I am using only uppercase letters A - Z, (65 - 90 in unicode). The trick to this is to wrap the letters in a circle. Meaning after Z comes A, and before A comes Z. Just like a circular lock would have in real life. A lock can also be set with a tolerance, say the tolerance is 1, the lock combo of CAT can be opened with CAT, CBT, BAT, DAT etc.... the tolerance doesn't have a limit so it could be 5. Another example, a combination is ZAZ, and the tolerance is 5, this can be opened with ZAZ, AZA, ZAD, etc.. you get the point. I am having trouble with the wrapping of the code, I got the tolerance to work for most of the combos, but I am getting that this is the wrong combo when I try, the combo is ZAZ, the tolerance is 5, and ZAD will not open, which it should. Here is my method which determines whether the lock will open..
where letterx is the letter which was used to set the lock. e.g. CAT, C = letter1, A = letter2, T = letter3. (The real lock combo)
where inputx is the letter which was used as an attempt by the user to open the lock. (The combo attempted to open the lock)
where CHAR_RANGE is 'Z' - 'A' which is 90 - 65 = 25.
and tolerance is the tolerance as explained in the paragraph above.
Anyways i have been over my head and tried nearly everything, I am still fairly new so if anyone can guide me in the right direction that would be great.
I have finally figured out what I wanted to do and the program works as expected. Although, I would like an experienced pair of eyes to look over the program and tell me if I could improve anything, maybe i have more code than I really need. etc..
So if you see anything, let me know. If it looks good, let me know .
This code generates a random subtraction equation and waits for user input. If input is correct answer it generates another equation until the user gets it wrong. When the user input is wrong, it displays the correct answer and it shows the score (which is the number of correct streaks the user went on before getting it wrong.) I am at the point where the the user gets the answer wrong and the program asks whether the user wants to play again. I am wondering how to make it so if the user types 'y' for yes, the whole program starts from the beginning. Else if he states 'n' for no, the program terminates. Below is my code:
Campbell Ritchie wrote:I think you will have to query that. You would have to change it to myLock.set('C', 'A', 'K'); or similar.
If that was the case and i could change myLock.Set() to take three arguments, I would be done a long time ago. Thing is people in my class are done this assignment and they didn't change any of the prewritten code. I will have to clarify with the prof. Thanks though!
In the LockTester code, it uses the set method to only set 1 char. This I can not change as it was given to me prewritten. That's what i'm lost in, if I don't figure it out here I will talk to the prof.