编写java程序,输入个数不定的整数,输入0时结束.统计这些整数中正数和负数的个数,并计算它们的总和.
来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/04/30 21:38:13
编写java程序,输入个数不定的整数,输入0时结束.统计这些整数中正数和负数的个数,并计算它们的总和.
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 函数中调用即可
编写java程序,输入个数不定的整数,输入0时结束.统计这些整数中正数和负数的个数,并计算它们的总和.
计算机VF编写程序题1:输入若干个非零任意数,分别统计其中正负数的个数,并求出他们的和.2:计算邮费:邮包重量
输入一组非零整数(以输入零作为输入结束标志)到一维数组中,设计程序,求出这组数的平均值,并统计出正数和负数个数
用pascal编写程序 输入十个整数,统计大于0的数的个数.
编写java程序,输入若干个正数,计算它们的平均值并统计比平均值大的实数的个数.
连续输入若干个整数,输入0结束.统计其正整数的个数,并计算其中正整数的总和、平均值并输出.c++
编写Matlab编写程序,要求从键盘输入若干个数,当输入0时结束输入,求这些数的平均值和它们之和.
C-free基础编程1.编写程序对输入的一批整数(以0作为数据输入结束的标志)进行统计,统计出正数的个数、负数的个数、奇
从键盘输入一组非0整数,以输入0标志结束,求这组整数的平均值,并统计其中正数和负数的个数.
编一个程序,输入10个整数,统计并输出其中正数、负数和零的个数.用指针方式
编一个程序,输入10个整数,统计并输 出其中正数、负数和零的个数.
用C语言编写程序.输入一些整数,编程计算并输出其中所有正数的和,输入负数或零时,表示输入数据结束.