I believe all of my logic is sound, but syntax is off a bit...
-ask for total numbers
-for loop to input total numbers
-1)input first number
-2)throw exception "must be positive"
-3)if negative > back to 1
-4)if no exception > calculate average
This is my first program using exception handling, so go easy :).
import java.util.Scanner;
public class Average {
public static void main (String[] args)
{
try
{
int n, total, sum, average;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter total numbers to be entered");
total = keyboard.nextInt( );
for (i=0;i<total;i++)
{
System.out.println("Enter N");
n = keyboard.nextInt( );
if (n<0)
throw new BadInput("N must be positive");
sum = n + sum;
average = sum/total;
System.out.println("Average is " + average);
}
}
catch (BadInput e)
{
String message = e.getMessage();
System.out.println(message);
anotherChance();
}
}
public static void anotherChance()
{
System.out.println("Enter N");
n = keyboard.nextInt();
}
public static void BadInput()
{
super("BAD INPUT; must be positive!");
}
}