package com.codegym.task.task05.task0507;
/*
Arithmetic mean
*/
public class Solution {
public static void main(String[] args) throws Exception {
int sum = 0;
int count = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int number = Integer.parseInt(br.readLine());
while (number != -1)
{
sum += number;
count++;
}
double mean = sum/count;
System.out.println(mean);
}
}
Could You help me, Guys. I give up.
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Andrei
25 January 2020, 09:44
// That solution works for me.
public static void main(String[] args) throws Exception {
double sum = 0;
int count = 0;
double mean;
for (int i = 1; ; i++)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(reader.readLine());
if (num != -1)
{
count++;
sum = sum + num;
}
else if (num == -1)
{
mean = sum / count;
System.out.println(mean);
break;
}
}
0
Andrei
24 January 2020, 21:05
thank you guys. trued your hints. but anyway compiler says all the time "cannot find simbol", and points BufferedReader class. maybe to use Scanner or what?
0
HaeWon Chung
24 January 2020, 20:26
The reason it doesn't run as expected is because you get the input only once before while code block is executed. You need to put
inside while block. And as Andason says change variable type of sum from int to double. I don't know how math works in Java but int divide by int will result in int variable type even if you are trying to store it in double. 0
Andason
24 January 2020, 20:10
try changing the sum to type double
and make the while block look like
double sum = 0.0, count = 0;
while(true)
{
int x = Integer.parseInt(in.readLine());
if(x == -1) break;
sum += x;
count++;
}
0