当我输入五个负整数时,如何停止输入过程? [英] How Do I Stop The Input Process When I Input Five Negative Integer Numbers?
本文介绍了当我输入五个负整数时,如何停止输入过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
main()
{
int a[20],x;
int n=a[0];
for(x=0; x<20; x++)
{
cout<<"Enter integer : ";
cin>>a[x];
while(a[x]<0)
{
cout<<"Enter integer : ";
cin>>a[x];
}
}
这应该是输出
输入:1
输入:1
输入:1
输入:1
输入: 1
输入:1
输入:1
输入:1
输入:1
输入:1
输入:1
输入:1
输入:1
输入: - 1
输入:-1
输入:-1
输入:1
输入:-1
输入:1
输入:-1
停止!
This should be the output
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: -1
Input: -1
Input: -1
Input: 1
Input: -1
Input: 1
Input: -1
Stop!
推荐答案
在for循环中使用计数器变量
use a counter variable inside the for loop
int countNegative=0
if(a[x]<0)
countNegative++
当 countNegative
变为5时使用break语句
use a break statement when the countNegative
becomes 5
if(countNegative==5)
break;
这篇关于当我输入五个负整数时,如何停止输入过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文