Can anyone help me with overloading. I have a task that starts with 2 numbers being entered by the user. then it increases to three numbers entered then 4 . how can I get the user input then have the program automatically figure the average of the numbers entered. Please help
Code:import java.io.*; import java.util.*; class FinalAverage { 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,x); Sum1.Sum(x, x,x); sum = x + x; } 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); } }




Reply With Quote
