javajava语言程序设计基础篇4.1
发布网友
发布时间:2022-07-08 20:14
我来回答
共2个回答
热心网友
时间:2022-07-14 00:14
import java.util.*;
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count1 = 0; //正数计数器
int count2 = 0; //负数计数器
float sum = 0; //和
ArrayList<Integer> a = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Enter an int value,the program exits if the input is 0:");
while(true) {
int num = input.nextInt();
if (num!=0) a.add(num);
else break;
}
input.close();
for(int i=0;i<a.size();i++) {
if(a.get(i)>0) count1++;
else count2++;
sum += a.get(i);
}
System.out.println("The number of positives is:"+count1);
System.out.println("The number of negatives is:"+count2);
System.out.println("The total number is:"+a.size());
System.out.println("The average is:"+sum/a.size());
}
}
追问运行不出
追答import java.util.*; //这句有没有漏写
System.out.println("The total number is:"+a.size());//这句是计数应该改成下面那句求和,看错题目了
System.out.println("The total is:"+sum);//求和
//我电脑运行一点错误都没有,不知道你啥错误提示
热心网友
时间:2022-07-14 00:14
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=1;//判断是否结束
int zhengshu_sum=0;//正数个数
int fushu_sum=0;//负数个数
float sum=0;//总和
while (a!=0){
Scanner sc = new Scanner(System.in);
System.out.println("Enter an int value,the program exits if the inputs is 0:");
a=sc.nextInt();
if(a>0){
zhengshu_sum=zhengshu_sum+1;
}else if(a<0){
fushu_sum=fushu_sum+1;
}
sum=sum+a;
}
System.out.println("正数个数:"+zhengshu_sum);
System.out.println("负数个数:"+fushu_sum);
System.out.println("输入之和:"+sum);
System.out.println("平均数:"+(float)(sum/(zhengshu_sum+fushu_sum)));
}追问运行不出