That leaves diplomacy, which Secretary of State Rex W. Tillerson and other officials have made clear is still the administration’s preferred course. If North Korea curbs its behavior, Mr. Tillerson said recently, there is a “pathway to sometime in the early future having some dialogue.”

Hours after Mr. Trump ruled out talks on Twitter, Defense Secretary Jim Mattis contradicted him. “We’re never out of diplomatic solutions,” he told reporters. In Geneva, Robert A. Wood, the American ambassador to the Conference on Disarmament, said the United States remained open to dialogue. “We do not seek to be a threat to the Kim Jong-un regime,” he said.

Trying to explain Mr. Trump’s tweet, Mr. Wood, who was once the State Department’s acting spokesman, said, “What the president is saying is that he doesn’t see talking as solving this problem and part of the reason is that the North is not interested in dialogue.”

Indeed, Mr. Trump’s sudden hostility to talks appeared to be less a reversal of his previous statements than an expression of frustration with Mr. Kim’s continued belligerence. Days after Mr. Trump praised him for his newfound restraint, Mr. Kim lobbed a missile over Japan.

For now, a Trump-Kim summit remains a far-fetched notion. Even if North Korea was interested in speaking to the United States, its string of belligerent actions — not to mention the June death of Otto F. Warmbier, the Ohio college student held for nearly 18 months in Pyongyang — would make a meeting politically untenable for Mr. Trump.

In his tweet, the president declared, “The U.S. has been talking to North Korea, and paying them extortion money, for 25 years.” While Mr. Trump’s precise meaning was unclear, he seemed to be referring to the promises of fuel oil, nuclear-power reactors, humanitarian aid and the lifting of sanctions that accompanied previous diplomatic negotiations.

Mr. Trump, experts said, is correct that talks with North Korea — whether conducted by Democratic or Republican administrations — have been costly and unproductive. And with the North Koreans now capable, by some estimates, of producing an atomic bomb every sixth or seventh week, the cost of reaching any new agreement would be even higher.