ANNAPOLIS, Md. — President Obama used a commencement speech before Naval Academy graduates on Friday to urge them to follow their “inner compass” and to warn that rising numbers of sexual assaults in the military threatened to erode America’s faith in the armed forces.

The president praised the military as the nation’s “most trusted institution,” but took note of the recent cases in which service members have been charged with sexual assault. He said those people “threaten the trust and discipline which makes our military strong.”

“We need your honor, that inner compass that guides you,” the president said, essentially using the platform at Navy-Marine Corps Memorial Stadium to scold those who have strayed from that direction recently. “Even more than physical courage, we need your moral courage — the strength to do what’s right, even when it’s unpopular.”