C++ question-「編寫一個程序,用於計算學生父母的貢獻總額,學生的父母同意增加學生的儲蓄學生使用下面給出的時間表保存的百分比「如果我用來查明父母的貢獻,這是if/else。除了使用switch語句之外,我現在必須再次創建該程序。我不知道如何做到這一點。用戶輸入總收入和他決定放棄的金額。 (我的課程剛剛開始,所以我必須用很簡單的方法來做到這一點謝謝)這是第一個版本:C++,將if/else改爲switch語句
percent_saved = money_saved/money_earned; // calculates the percent of how much was saved
if (percent_saved <.05) // this if/else if statement assigns the parents percentage of contribution to their students saving
{
parents = .01;
}
else if (percent_saved >= .05 && percent_saved < .1)
{
parents = .025;
}
else if (percent_saved >= .1 && percent_saved < .15)
{
parents = .08;
}
else if (percent_saved >= .15 && percent_saved < .25)
{
parents = .125;
}
else if (percent_saved >= .25 && percent_saved < .35)
{
parents = .15;
}
else
{
parents = .2;
}
parentsmoney = parents*money_earned; // using the correct percentage, this creates the amount of money parents will contribute
total_savings = parentsmoney + money_saved; // this adds together the parent's contribution and the student's savings
+1,因爲這是一個很好的合理答案。 – 2014-09-25 00:54:46