代码如下:
import java.util.Scanner;
class TestForWhile {
public static void main(String[] args) {
//注:无限循环的条件for(;;){},while(true){}
Scanner scan = new Scanner(System.in);
int count=0;//这是计算正数的个数
int sum = 0;//这是计算负数的个数
for(;;){
System.out.println("请输入一个数:");
int num = scan.nextInt();
if(num>0){
count++;
}
else if(num<0){
sum++;
}
else{
System.out.println("该程序结束!!!!!");
break;
}
}
System.out.println("正数个数为:"+count+"个");
System.out.println("负数个数为:"+sum+"个");
}
}