(Java) Phrasing the question has been a bit difficult, but this is what I mean:
I've was working on a simple Ceasar Cipher that, based on whether to encrypt or decrypt, should either add or subtract the key to/from the value associated with the character. As such, this is what I came up with:
// if direction == true, shift forwards (encrypt), else shift backwards (decrypt)
if (direction == true) {
newPos = (character - 'a' + key) % 26 + 'a';
} else if (direction == false) {
newPos = (character - 'a' - key) % 26 + 'a';
}
Yet I was wondering if this could be shortened, so that it would place either a "-" or a "+" symbol inside the calculation based on whether it should encrypt or decrypt, for example like this: (I know the following code doesn't work, but it illustrates what I mean)
// if direction == true, place a '+', else place a '-'
newPos = (character - 'a ' ((direction == true) ? + : -) key) % 26 + 'a';
Thus I was wondering if there is any way to do something like this in Java? I'm not sure if this would have an enormous benefit on keeping my code clean, but I figured it might help eliminate some repetition in the code. As of yet, I've been unable to find an answer to this online.
Edit: I don't necessarily need a solution for the above-described scenario (that was just how I came across the topic), but I meant this post to be a more general question about if this would be possible and how.
Aucun commentaire:
Enregistrer un commentaire