【发布时间】:2018-03-07 20:54:46
【问题描述】:
我的任务是打印一组从 min 到 max 的数字(用户输入 min 和 mix),但每隔三个数字打印一个“x”。我不确定如何设置它。我的朋友建议使用 count++,但我无法让它正常工作。它运行但不显示任何 X。
#include <iostream>
#include <iomanip>
using namespace std;
void no_5_count_from_min_to_max_skip_two(int min, int max);
int main()
{
int min;
int max;
int first;
int second;
cout<<"Enter first number:";
cin>>first;
cout<<endl;
cout<<"Enter second number:";
cin>>second;
cout<<endl;
if (first>second){
max = first;
min = second;
}
else{
max = second;
min = first;
}
no_5_count_from_min_to_max_skip_two(min,max);
return 0;
}
void no_5_count_from_min_to_max_skip_two(int min, int max){
cout<<"5.Counting from min to max but skip two:";
cout<<endl;
for(int i=min; i<=max; i++){
int count = 0;
count++;
if (count==3){
cout<<setw(4)<<"X";
count = 0;
}
cout<<setw(4)<<i;
}
cout<<endl;
}
【问题讨论】:
-
int count = 0;每次循环都会重新创建和重置count。将其移到for循环之外。
标签: c++ loops include counter iostream