题目
编写java程序,输入个数不定的整数,输入0时结束.统计这些整数中正数和负数的个数,并计算它们的总和.
提问时间:2021-02-12
答案
public static void test(){
Scanner sc = new Scanner(System.in);
long num = 0,negative = 0,positive = 0,sum = 0;
List nums = new ArrayList();
do{
System.out.println("please enter a number :");
String s = sc.nextLine();
if (isNumber(s)) {
num = Long.parseLong(s);
if(num != 0){
nums.add(num);
continue;
}
break;
}
System.out.println("not number !");
break;
}while(true);
for (Long n :nums) {
if(n > 0){
positive ++;
}else{
negative ++;
}
sum += n;
}
System.out.println("the negative :" + negative);
System.out.println("the positive :" + positive);
System.out.println("the sum :" + sum);
}
public static boolean isNumber(String s){
try {
Long.parseLong(s);
return true;
} catch (Exception e) {
return false;
}
}
在 main 函数中调用即可
Scanner sc = new Scanner(System.in);
long num = 0,negative = 0,positive = 0,sum = 0;
List nums = new ArrayList();
do{
System.out.println("please enter a number :");
String s = sc.nextLine();
if (isNumber(s)) {
num = Long.parseLong(s);
if(num != 0){
nums.add(num);
continue;
}
break;
}
System.out.println("not number !");
break;
}while(true);
for (Long n :nums) {
if(n > 0){
positive ++;
}else{
negative ++;
}
sum += n;
}
System.out.println("the negative :" + negative);
System.out.println("the positive :" + positive);
System.out.println("the sum :" + sum);
}
public static boolean isNumber(String s){
try {
Long.parseLong(s);
return true;
} catch (Exception e) {
return false;
}
}
在 main 函数中调用即可
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
- 1描写自然环境的词语
- 2when it ___ school work,the professor will treat his students equally.答案是comes to evaluating
- 3伴随矩阵的值和原矩阵的值有什么联系?
- 4从0、1、2、6中选3个数组成三位数,使它是3的倍数
- 5一个三角形的三条边都是整厘米数,已知其中的两条边分别是7厘米和9厘米,那么第三条边最长是( )厘米,
- 6get的过去式,过去分词?
- 7explain和state的区别是什么
- 8一道高中关于万有引力的物理题..
- 970元9角-58元5角=()元 4米53厘米+17米37厘米=()米 36平方米-24平方米3平方
- 10已知二次函数y=x*2-mx-4与x轴相交的两个交点及抛物线的顶点组成一个等边三角形,求其关系式
热门考点
- 1行列式第一行0 1 1 ...1 1第二行1 0 x ...x x第三行1 x 0 ...x x倒数第二行1 x x...0 x
- 2He has lived in the small house____ 30years..加什么?
- 31+X+X×X=91
- 4初三物理武汉号在巡航时,通过舰载雷达发现了目标,若雷达发出电磁波经2*10-4S收到了回波,则目标距军舰的
- 5写乡村生活的作文要400字左右
- 6在等三角形abc中,ab=ac=6,角abc=15度,求等腰三角形的面积是多少?
- 7ride----a horse live-----trees drive----a car walk-----the ground用介词填空
- 8声源离障碍物太近,则回声可以使原声_________.
- 9GS263-C4/0.03 ZRBV5*6 SC32 FC代表什么含义
- 10求由方程y=xe^y+1所确定的隐函数的导数?