The question i've always asked, why detonate over a city. Why not on a nearby coastal region where the US can "flex their muscle" but refrain from unecessary loss of life to innocent people. I think both would have had the same impact.
It probably has been explained a few times on this thread. Here another one.
The US wanted to finish the war immediately. They didn't want to flex their muscle and show that they are stronger, they had already done that and Japan was going to be defeated anyway. Both the US and Japan knew that. However, Japan was not planning to surrender, and that was causing problems. The casualty rates in the Battle of Okinawa for the US were too high (and for Japanese far higher). Too many US soldiers were killed. And while an afterthought to the US, too many Japanese (both soldiers and civilians) were getting killed. At that stage, the US had the way of finishing the war ASAP, instead of having hundreds (or even thousands) of their soldiers being killed every day. There was the option of continuing it indefinitely, and at some stage, Japan going into guerrilla warfare. There were estimates about it, probably half a million US soldiers, and I think up to 7-12m Japanese. To further complicate things, the Soviet Union had already reached Manchuria, overthrown the Japanese-backed government, and was planning their grand invasion in Japan. Not only this would have made things worse (even more people killed) but could have put the US and Soviet Union against each other and cause another war.
And here come the atomic bombs. Sure, the US could have threatened Japan with them and detonate them in the ocean or in some coastal region without people. The problem though is that the US didn't have many of them to 'waste'. In fact, they had only 3 nukes (the third one was planned to be used too if Japan doesn't surrender). A historian might correct me, but from what I remember when reading about this, the next set of nukes wouldn't be ready until October or so, potentially delaying the end of the war for another 2 months (on numbers, that would be tens of thousands of US soldiers, and as an afterthought, a shitload of Japanese people, far more than from nukes). So yep, strategically speaking, wasting one of your three aces to threaten Japan was not a good idea. The second problem was that the Japanese were extremely stubborn, they had no intention whatsoever to surrender. While people ask 'why the US used the second nuke', not many ask 'Why Japan did not surrender after the first nuke'. They knew what was happening and still continued. Then, the US used the second nuke. Did Japan immediately surrender? Well, the Emperor and a part of the government wanted so. The other part didn't want to and did a coup d'etat to imprison the Emperor and continue the war. Luckily for them, the coup d'etat failed, and Japan surrendered. So to answer your questions, even after the usage of the two nukes, there was a non-trivial chance that Japan was going to continue the war.
People see the nuke destruction and say bad, but the war was bad too. More people died from Tokyo bombing (or from Okinawa battle) than in either Nagasaki or Hiroshima. More would have died if the war didn't end. Japan's infrastructure was destroyed a lot, causing people to die from starvation even after 2 years after the war ('Grave of the Fireflies' treats that topic), many more would have died if the war continued, and Japan's resurrection would have needed far more time.
A lot of people think 'nukes are bad', and in recent time 'the US is bad' (Caf has pathological hate about the US), add 2+2=10 concluding that the US detonated the nukes for shit and giggles and it was morally the wrong thing to do. Sad as it was, it was by far the best option. Any other option would have caused significantly more deaths and pain, with Japan needing far longer to get back to normal life. And a president who would have signed the death of tens of thousands of his people (many more, if the US refused to use nukes and decided to conventionally defeat Japan) would have been morally wrong, and in my book, a traitor. Fortunately, Truman wasn't buying that shit, and fortunately (for Japan too), he ended the war. Yes, up to a quarter of a million people died and that was horrendous. Bear in mind, that another 60 million were killed during that war (that is close to 250 people dying in the war for every person getting killed by the nukes). And in China alone, Japan caused the deaths of near 20 million people, which is far worse than whatever nukes did.