The U.S. Department of Defense decision to terminate F-22 production at 187 aircraft resulted in each aircraft costing (including development and production spending), $332 million. Just the production costs of the last F-22s built was $153.2 million. Added to the cost of the last few aircraft was a $147 million fee the Department of Defense agreed to pay if the production line was shut down. This goes to pay for shutting down facilities and terminating contracts with hundreds of supplies.
The F-22 is a superb aircraft, probably the most capable fighter in the world. But the development and manufacturing costs kept rising until it became too expensive for the media, voters and politicians. The air force was able to build it, but they couldn't sell it to the people who paid the bills.
A decade ago, the F-22 was a $62 billion program, of which development accounted for $18.9 billion (this was a spending cap imposed by Congress). A decade before that, the air force was planning to buy 750 F-22s. Costs kept going up for two decades, and Congress refused to provide more money. So, for $62 billion, the air force ended up getting fewer aircraft.
The air force ran into a similar problem with the B-2 bomber, which became so expensive they were only allowed to build 21, and these cost $2.1 billion each. . About half of that was development expense. Actual construction costs for each of those aircraft was about $933 million each. Still pretty high, mainly because a lot of special machinery and factories had to be built to manufacture the many custom components.
The air force likes to point out that if the original (1986) plan had been followed, each B-2 would have cost $438 million each. But then the entire program would have cost $58.2 billion, versus $44.3 billion for the 21 plane program (which included $10 billion more R&D expense).
New technology gives a weapon, especially an aircraft, an edge in combat. But since World War II, most military technology has been developed in peacetime conditions. This means it is more than twice as expensive, as there is no wartime urgency to overcome bureaucratic inertia (and emphasis on covering your ass, which is very time consuming and expensive) and hesitation (because you don't have a war going on to settle disputes over what will work best). Developing this technology takes longer in peacetime, which also raises the cost, and fewer units of a new weapon are produced (driving up the amount of development cost each weapon will have to carry.) If several hundred B-2s were produced under wartime conditions, each aircraft would have probably cost $200 million, or less. In other words, a tenth of what it actually cost. Same deal with the mythical $35 million F-22, or any other high tech weapon.
Other nations have adapted more effectively to peacetime development conditions. But the United States has the largest amount of peacetime military research and development, and this has created a unique military/industry/media/political atmosphere.