我不想在最后一个 number/value 之后有一个逗号,我该如何删除它(C++,循环)
I don't want a comma after the last number/value, how do i remove it (c++, loop)
所以,我朋友的程序应该找到数字 3 的乘法,然后在数字之间用逗号打印出来。而不是最后一个。
int a;
int b;
cout<< "First Number = ";
cin>>a;
cout<< "Last Number = ";
cin>>b;
if(a<=b)
{
for(a;a<=b;a++)
{
if(a%3 == 0 && a%2 != 0)
{
cout<<a;
}
if(a<b && a%3==0 && a%2 != 0)
{
cout<< " , ";
}
else if(a==b)
{
cout<< ".";
}
}
}
然后我输入
一=1
b = 20
这是我所期望的
3 , 9 , 15.
这就是我实际得到的
3 , 9 , 15 , .
之前谢谢大家,希望大家谅解
我会这样做:
char const* sep = ""; // item separator (nothing to begin with)
for(a;a<=b;a++)
{
if(a%3 == 0 && a%2 != 0)
{
cout << sep << a; // output the separator before the next value
sep = ", "; // now change it to a comma
}
}
cout << "."; // finish with a full stop
所以,我朋友的程序应该找到数字 3 的乘法,然后在数字之间用逗号打印出来。而不是最后一个。
int a;
int b;
cout<< "First Number = ";
cin>>a;
cout<< "Last Number = ";
cin>>b;
if(a<=b)
{
for(a;a<=b;a++)
{
if(a%3 == 0 && a%2 != 0)
{
cout<<a;
}
if(a<b && a%3==0 && a%2 != 0)
{
cout<< " , ";
}
else if(a==b)
{
cout<< ".";
}
}
}
然后我输入
一=1
b = 20
这是我所期望的
3 , 9 , 15.
这就是我实际得到的
3 , 9 , 15 , .
之前谢谢大家,希望大家谅解
我会这样做:
char const* sep = ""; // item separator (nothing to begin with)
for(a;a<=b;a++)
{
if(a%3 == 0 && a%2 != 0)
{
cout << sep << a; // output the separator before the next value
sep = ", "; // now change it to a comma
}
}
cout << "."; // finish with a full stop