我想仅仅输入最后一个结果而不是所有结果,为了解释这里我的代码我尝试了但是没有工作:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string boo="blablab";
int count=0;
int b=0;
for (size_t i = 0; i < boo.size(); i++)
{
while (boo[i] == 'a')
{
count+=i+1;
if(count>b) b=count;
cout<<<<"a="<<b<<"\n";;
break;
}
}
system("pause");
return 0;
}
而不是结果: A = 3 A = 9 我想要它的结果: A = 9
答案 0 :(得分:2)
只需将cout
放在for
循环之外,就像这样:
for (size_t i = 0; i < boo.size(); i++)
{
while (boo[i] == 'a')
{
count+=i+1;
if(count>b)
b=count;
// cout<<<<"a="<<b<<"\n";; //remove from here
break;
}}
//add here
cout<<<<"a="<<b<<"\n";;