本文主要是介绍编写程序,读入未指定个数的整数,判断读入的整数有多少个,读入的负数有多少个,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
编写程序,读入未指定个数的整数,判断读入的整数有多少个,读入的负数有多少个,输入0表示程序结束,计算总和和平均值。
import java.util.Scanner;
public class chapt3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an int value,the program exists if the input is 0:");
int value=0;
int total = 0;
int positives = 0;
int negatives = 0;
double average = 0;
int sum = 0;
do{
value =input.nextInt();
if(value > 0)
{positives++;sum = sum + value;}
if(value<0)
{negatives++;sum = sum + value;}
}while(value!=0);
total=positives+negatives;
average = (double)sum / total;
System.out.println("The number of positives is "+positives);
System.out.println("The number of negatives is "+negatives);
System.out.println("The totalsum is "+sum);
System.out.println("The average is "+average);
}
}
这篇关于编写程序,读入未指定个数的整数,判断读入的整数有多少个,读入的负数有多少个的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!