我已經在Code :: Blocks上創建了這個測試分數程序,根據最大可達點數和他們在測試中達到的分數計算學生測試的百分比,但是我得到的所有情況都是0%結果,我不知道爲什麼。爲什麼在這個程序中所有情況下都會得到0%的結果?
有人可以幫我解釋一下嗎?
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
//enter the maximum reachable score
int maxscore;
cout << "Enter the highest possible score: ";
cin >> maxscore;
//enter the reached score
int score;
cout << "Enter your score: ";
cin >> score;
//calculate percentage
//what's wrong here with the percentage calculation?
int percentage;
percentage = (score/maxscore)*100 ;
//output the results (followed by a NewLine)
cout << "Your result is: ";
cout << percentage <<"%"<< endl;
//wait until user is ready before terminating the program to allow the user
//to see the program results
cout << "Pres Enter to continue..."<<endl;
cin.ignore(10, '\n');
cin.get();
return 0;
}
關鍵字:整數算術,」score/maxscore「d oes不會產生你期待的結果 – VTT
@VTT那麼它將如何工作? –
通過將int百分比更改爲浮點百分比 – Kartik