當前位置:編程學習大全網 - 編程語言 - c++程序沒有出現輸出

c++程序沒有出現輸出

最簡單的回答:“interest2=(money+interest2)*0.05”這個代碼有問題,怎麽有問題法,自個去想,反正,這個代碼會讓interest2增加得越來越少,最後幾近不增加。

****詳述如下,有耐心可以看看,應該對妳有幫助****

1、個人理解,妳給出的代碼中,計算的思維方式是有問題的。妳在這個編程設計中,應該是想計算著復利。

2、復利的計算工式應該是 “interest2=interest2*(1+0.05)” 其中的0.05是利率,而不是“interest2=(money+interest2)*0.05”。

3、“interest2=(money+interest2)*0.05”這個式子的直接後果是讓interest2增加了壹個非常小的數。最後變化到“5.26315789473684”這個值,在double數據類型下,這個值加上100,得到105.2631578947368,再乘0.05得到5.263157894736842,如果壹直這樣下去,它會因為受精度的限制,壹直就是這麽大。

4、所以,妳的程序就會進入壹個死循環。

5、授人以魚不如授人以漁,以後碰到問題要學會如何使用調試。下面就是我針對妳的程序給出的調試代碼,供妳參考,這個調試代碼會將關鍵的數據變化的輸出展示出來,在代碼無問題後妳再將不需要的輸出註釋掉:

#include<iostream>

#include<iomanip>?//用於控制輸出精度調整

int?main()

{

using?namespace?std;

const?int?money?=?100;

double?interest1,?interest2;

interest1?=?money*0.1;

interest2?=?money*0.05;

cout?<<?setprecision(32);?//控制輸出精度到32位

int?i;

for?(i?=?1;?interest1>interest2;?i++)

{

interest2?=?(money?+?interest2)*0.05;

cout?<<?i?<<?"--"?<<?interest1?<<?"--"?<<?interest2?<<?"--"?<<?money?<<?"--"?<<?"\n";?//輸出執行情況

system("pause");?//用於暫停以查看,在windows及dos下有效。

}

cout?<<?"第"?<<?i?<<?"年後c的投資價值將超過d"?<<?"d的投資價值為"?<<?interest1?+?money?<<?endl?<<?"c的投資價值為"?<<?interest2?+?money?<<?endl;

return?0;

}

6、至於正確的代碼,個人以為,妳將

interest2 = (money + interest2)*0.05;

改成

interest2=interest2*(1+0.05);

即可。

因為不知道妳具體的設想,所以,只是個人以為,請根據妳的目標要求修正。

  • 上一篇:嶽陽中南工業技術學校有什麽專業
  • 下一篇:?6?1奧斯卡的來歷和歷屆奧斯卡獲獎影片是什麽
  • copyright 2024編程學習大全網