This is java class Two thing of this homework 1 the code ha
This is java class.
Two thing of this homework:
1 . the code has part \" array\" in the code., negative number >>> you typed it wrong
2 when you have avarage number ,I need the this kind: 5.2, 3.7 4.9. 2.1 3..... when you enter : any nopostitive number . example: -1, -2, -3. -4 >>> then and then afterward must enter all N numbers. Use an array to save the N integers. If the Write a program that calculates the average of N integers. The program should prompt the user to enter the value for N and then afterward must enter all N numbers. Use an array to save the N integers. If the user enters a nonpositive value for N, then an exception should be thrown (and caught) with the message \"N must be positive.\" If there is any exception as the user is entering the N numbers, an error message should be displayed, and the user prompted to enter the number again.
Solution
/* package whatever; // don\'t place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int n=0;
int i;
int [] arr= new int[20];
Scanner in = new Scanner(System.in);
try {
System.out.print(\"Enter value to N: \");
n = in.nextInt();
if( n < 0 )
throw new Exception(\"N must be positive.\");
}catch(Exception exc)
{
System.out.println(\"Error: \" + exc.getMessage());
}
System.out.print(\"Enter value to array: \");
try
{
for(i=0;i<n;i++)
{
arr[i]=in.nextInt();
if(arr[i]<0)
{
throw new Exception(\"Can not enter negative number in array!!!\ Please Enter numbers in array again\ \");
}
}
}catch(Exception e)
{
System.out.println(\"Error: \" + e.getMessage());
}
}
}
