求平均值(无限for循环)
程序代码:
//求平均值(无限for循环) #include <iostream> using namespace std; int main( ) { double value = 0.0; double sum = 0.0; int i = 0; char indicator = 'n'; for( ; ;) //无任何表达式,只有两个分号 { cout << endl << "Enter a value: "; cin >> value; ++i; sum += value; cout << endl << "Do you want to enter anolther value ( enter y or n) ? "; cin >> indicator; if ((indicator != 'y') && (indicator != 'n')) cout << endl <<"Your enter is worng !" << endl; if((indicator == 'y') || (indicator == 'Y')) cout << endl << "ok!"; if((indicator == 'n')|| (indicator == 'N')) break; } cout << endl << "The average of the " << i << " Values you entered is " << sum / i << endl; system("pause"); return 0; }
值得注意的 for 循环。