Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

confused about overloading, loops

$
0
0
I am trying to complete a task for overloading and I have the code below. my task is to be able to enter 2, 3 or 4 integers and have the program average them out. I have it set to enter 2 integers but I am a little confused on how to have it take as little or many integers as I want to enter, then average them out. Please help?
import java.io.*;
import java.util.*;


class Average	{
	
	
	void Sum(int num1, int num2)
	{
		System.out.println("Sum of "+ num1 + " and " + num2+ " is " +(num1 + num2));
	}
	void Sum(int num1, int num2, int num3)
	{
		System.out.println("Sum of "+ num1 + " and " + num2+ " and " + num3+ " is " +(num1 + num2 + num3));
	}
	
	


	
	
		public static void main(String[] args) {
			float average;
			int count = 0, x,x2, sum = 0 ;
			
			Average Sum1 = new Average();
			Scanner input = new Scanner(System.in);
		
			try	{	
				System.out.println("Enter first number to find the Average ");
				x = input.nextInt();
					
							System.out.println("Enter second number to find the Average ");
					count = 1;
					x2 = input.nextInt();
					count++;
					Sum1.Sum (x,x2);
					sum = x + x2;	
				}
		 		catch (NumberFormatException e)
		 		{
		 			System.out.println("You did not enter an integer, and an Error occured. Please enter an integer?");
		 		}
			average = (sum/count);
			System.out.println("your average of the numbers entered equals......" + average);
			System.exit(0);
		}	
			
			}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>