Answer:
In 1945, Japan was cornered. American forces were in full swing towards the main islands of Japan, gaining Japanese positions on the Pacific islands. The main problem in this situation was that the Japanese did not admit to surrendering under any circumstances, but rather fought until total annihilation, either their own or the enemy's. Thus, for example, battles such as that of Iwo Jima or Okinawa lasted for months, and involved tens of thousands of deaths of American soldiers, in addition to those of almost the entire Japanese forces.
This demonstrated to the American government that a battle in mainland Japan would be a massacre that could lead to millions of lost lives. For this reason, the Truman government made the decision to carry out a fast, precise and powerful attack that, causing large amounts of casualties and destruction, would send a message to the Japanese leaders urging them to surrender, thus avoiding the millions of casualties that could occur through conventional warfare on Japanese territory.
Therefore, the moral justification for the nuclear attack on Hiroshima and Nagasaki was to avoid a far greater number of deaths than those that would occur as a consequence of the attack.