E.g. Armies operate hospitals for it wounded soldiers. Can your software be used in an army hospital?
E.g. What happens if private company ABC Inc. Is using your software, and then they are contracted by the military to perform work - is that allowed? You say you "want to prevent any military from legally using it" - who's using your software in this situation ... the military or private company ABC?
EDIT: and even if you figure out the exact language to restrict military use, how do you enforce it and what are the consequences of breaking it?
EDIT2: Linked below is an example to restrict military use. Note though, it's so broad reaching that it might scare away even non-military organizations for using your software. And it still doesn't address how you enforce such license. So there's lots of questions about the applicable of this example license. http://web.cs.ucdavis.edu/~rogaway/ocb/license2.pdf
EDIT3: the problem you are going to have is that you're breaking a fundamental principle of open source software. And that is that anyone can use your software so long as they do so based on your stipulations. What you want to do is restrict who can even use your software. Read the FAQ for "what is open source software". https://opensource.org/faq