简体   繁体   English

Java加法程序,用于对输入的数字进行累加,直到输入零为止。 SumPositive和SumNegative的平均值

[英]Java addition program which keeps addup the numbers you input ing until you enter a zero. Average of SumPositive and SumNegative

In this java program you (the user) keeps entering numbers until you enter a zero, which is when the list terminates. 在此Java程序中,您(用户)将一直输入数字,直到输入零为止(这是列表终止的时间)。 It will compute the sum of positive even & sum of odd even and negative numbers. 它将计算正偶数之和与奇数偶数和负数之和。 It will also compute the average. 它还将计算平均值。

I'm stuck at the part to get the average of sum of even positive numbers, sum of odd positive numbers, and sum of negative numbers. 我被困在获取偶数和,奇数和负数之和的平均值上。

import java.util.*;
class sumPositiveAverage {
    public static void main (String[] args) {
        Scanner sc = new Scanner (System.in);
        System.out.println ("Enter numbers. List terminates when you enter a zero. 

        Enter a zero when you want to begin the addition.");
        int a = sc.nextInt();
        int esum=0;
        int osum=0;
        int nsum=0;
        while (a !=0)
        {
            if (a>0)
            {
                if (a%2==0)
                {
                    esum = esum+a;
                }// end of 3rd innermost if statement
                else
                {
                osum = osum+a;
                }// end of 3rd else statement
            }//end of 2nd middle if-else-loop
            else if (a<0)
            {
                nsum=nsum+a;
            }//end of 2nd middle else statement
        }//end of while loop
        System.out.println ("The sum of even positive numbers is "+esum);
        System.out.println ("The sum of odd positive numbers is "+osum);
        System.out.println ("The sum of negative numbers is "+nsum);
    }//end of main
}//end of class2
import java.util.*;

public class sumPositiveAverage {

public static void main(String[] args)
{
   int sum_even = 0;
   int sum_odd = 0;
   int sum_negative = 0;
   int sum_positive = 0;

   int count_even = 0;
   int count_odd = 0;
   int count_negative = 0;
   int count_positive = 0;

   double avg_sum_even = 0;
   double avg_sum_odd = 0;
   double avg_sum_negative = 0;
   double avg_sum_positive = 0;

   int sum = 0;
   int count = 0;
   Scanner input = new Scanner(System.in);
   int data = 0;
   do
   {
       System.out.print("Type in a positive or negative number and press enter key, if 0 is entered program stops: ");
       data = input.nextInt();


       if(data > 0){

           if(data%2 == 0){
           //even number
           sum_even = sum_even + data;
           count_even++;
           }else{

               //odd
               sum_odd = sum_odd + data;
               count_odd++;

           }

           sum_positive = sum_positive + data;
           count_positive++;
       }else if(data < 0) {

           //negative number
       sum_negative = sum_negative + data;
       count_negative++;

       }


    }
    //Stops if the value of data is ZERO(0) and continues if it's not
    while(data != 0);

   //here means zero has been entered

     if(count_positive > 0) { avg_sum_positive = (double)sum_positive/count_positive; }
     if(count_negative > 0) { avg_sum_negative = (double)sum_negative/count_negative; }
     if(count_even > 0) { avg_sum_even = (double)sum_even/count_even; }
     if(count_odd > 0) { avg_sum_odd = (double)sum_odd/count_odd; } 


     System.out.println("Sum of Positive Numbers = " + sum_positive);
     System.out.println("Sum of negative Numbers = " + sum_negative);
     System.out.println("Sum of odd Numbers = " + sum_odd);
     System.out.println("Sum of even Numbers = " + sum_even);

     System.out.println("Count of Positive Numbers = " + count_positive);
     System.out.println("Count of negative Numbers = " + count_negative);
     System.out.println("Count of odd Numbers = " + count_odd);
     System.out.println("Count of even Numbers = " + count_even);

     System.out.println("Average of Positive Numbers = " + avg_sum_positive);
     System.out.println("Average of negative Numbers = " + avg_sum_negative);
     System.out.println("Average of odd Numbers = " + avg_sum_odd);
     System.out.println("Average of even Numbers = " + avg_sum_even);


}//closing main
} // closing class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个加法程序,它会不断累加您输入的数字,直到您输入零为止。 在Java中 - An addition program which keeps adding up the numbers you input until you enter a zero. In Java 程序让用户输入数字直到输入零,然后打印总数、输入次数和平均值 - Program lets the user enter numbers until they enter zero and then prints total, number of inputs, and average 如何将AVERAGE和LETTERGRADE合并到具有2D数组的Java程序中 - How do you incorperate an AVERAGE and LETTERGRADE in a java program which has 2D Array 循环直到在Java中输入零 - looping until enter zero in java JAVA中输入输出对话框怎么求三个数的平均值? - How do you find the average of three numbers with input and output dialog boxes with JAVA? 输入数字直到 0,然后计算平均值 - Input numbers until 0, then calculate average 如何在java中键入无效数字之前输入大量数据 - How to input a lot of data until you type in an invalid number in java 一个素数程序,允许用户测试数字,直到用户输入零。 但是,在测试6个数字后,它会打印错误的消息 - a prime number program that allows the user to test numbers till the user enters zero. However, after testing 6 numbers, it prints incorrect message 我如何在 JSP 中创建一个程序来生成您可以回答的随机加法方程,然后检查您是否正确? - How I create a program in JSP that produces random addition equations that you can answer which then checks to see if you're correct or not? 一个 Java 程序,带有一个循环,允许用户输入一系列整数,然后显示最小和最大数字 + 平均值 - A Java program with a loop that allows the user to enter a series of integers, then displays the smallest and largest numbers + the average
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM