我的代码将n减去一个数量:
<div class="container application-container">
<div class="row seven-rows">
<div class="column text-center image-container">
<img src="./images/aoe_icons_services_order_management.png" width='69' height='61' alt="e-commerce">
<p class="mt-2 text-custom">E Commerce</p>
</div>
<div class="dropup">
<div class="dropup-content pt-4 pb-4">
<div class="row">
<div class="col-lg-8 ml-5">
<h3> Innovative Omnichannel E-Commerce Solutions</h3>
<p>AOE develops flexible and high-performance Enterprise E-Commerce portals. Our solutions digitize business processes and create efficient, profitable platforms for providers and customers.</p>
<a class="btn btn-custom">E Commerce Solutions</a>
</div>
<div class="col-lg-4">
</div>
</div>
</div>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
但是不起作用。
程序运行:
int op = 0,quan,numbers,many;
cout << "Quantity to substract: " << endl;
cin >> quan;
cout << "Times to subs quantity:" << endl;
cin >> many;
for(int count = 1; count <= many; count++)
{
cout << "Insert " << count << " Number" << endl;
cin >> numbers;
op = quan - numbers;
}
cout << "Total: " << op << endl;
总数应为Quantity to substract:
10
Times to subs quantity:
5
Insert 1 Number:
1
Insert 2 Number:
1
Insert 3 Number:
1
Insert 4 Number:
1
Insert 5 Number:
1
Total:
9
您能支持我这个问题吗?谢谢
答案 0 :(得分:1)
看来这里的目标是从quan
中减去所有5个数字。有问题的代码仅减去最后一个。
要减去所有数字,请将结果变量初始化为第一个数字:
op = quan;
并在循环中,从结果变量中减去:
op = op - numbers; // alternatively: op -= numbers
答案 1 :(得分:1)
尝试一下:
int op = 0,quan,numbers,many;
cout << "Quantity to substract: " << endl;
cin >> quan;
cout << "Times to subs quantity:" << endl;
cin >> many;
for(int count = 1; count <= many; count++)
{
cout << "Insert " << count << " Number" << endl;
cin >> numbers;
op = quan - numbers;
quan = op; // Add this so that new value is assigned to quan
}
cout << "Total: " << op << endl;
答案 2 :(得分:0)
这是因为您从quan
减去而未使用op
的先前值。
每次执行以下操作时,都会在代码中使用op = quan - numbers
:您会丢失先前获得的状态。
int op = 0,quan,numbers,many;
cout << "Quantity to substract: " << endl;
cin >> quan;
cout << "Times to subs quantity:" << endl;
cin >> many;
// make op take the value of quan
op = quan;
for(int count = 1; count <= many; count++)
{
cout << "Insert " << count << " Number" << endl;
cin >> numbers;
// substract from op
op = op - numbers;
}
cout << "Total: " << op << endl;