0
Can someone help me with this code please? I need to calculate average of 5 inputs, I tried lot but it keeps giving errors
public class average { public static void main(string[]args) { IntStream Inputs = IntStream.of(10, 20, 30, 40, 50); OptionalDouble res = inputs.average(); system.out.println("average of inputs..."); if (res.isPresent()) { system.out.println (res.getAsDouble); } else { system.out.println ("nothing"); } } }
2 Answers
+ 7
See this if you dont want to take input from user u can define numbers in array
public class Main { public static void main(String[] args)
{
double num = 0; double x = 1;
Scanner sc = new Scanner(System.in); System.out.println("Input the number(n) you want to calculate the average: ");
int n = sc.nextInt();
while (x <= n)
{
System.out.println("Input number " + "("+ (int) x +")" + ":");
num += sc.nextInt(); x += 1;
}
double avgn = (num / n); System.out.println("Average:" + avgn); } }
0
I didn't understand that code đ„ I'm still soo new in java, I didn't even attend the classes when they started with java and now I actually need it for an exercise : (given you 5 inputs and "average" method, calculate the average value for the 5 inputs values inside the average method and return it) I need to give the exercise done today and didn't have much time to understand all about java in one night đ„đą