我的 rand() 為每個學生提供相同的數字 (GPA)。
srand(time(NULL));
int gpa = 0 (rand() % (10 - 0 1));
for (int i = 0; i < number; i ) {
cout << "Enter the student #" << i 1 << "'s name: ";
getline(cin, pStudents[i]); cout << endl;
}
for (int i = 0; i < number; i ) {
cout << "Student " << pStudents[i] << " has GPA of: " << gpa << endl;
}
uj5u.com熱心網友回復:
你只計算一次。
以下是如何在您的代碼上修復它:
srand(time(NULL));
for (int i = 0; i < number; i ) {
cout << "Enter the student #" << i 1 << "'s name: ";
getline(cin, pStudents[i]); cout << endl;
}
for (int i = 0; i < number; i ) {
int gpa = 0 (rand() % (10 - 0 1));
cout << "Student " << pStudents[i] << " has GPA of: " << gpa << endl;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/453468.html
