please tell me if i have done something wrong here. I need suggestion.
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
double sum = 0;
double count = 0;
double mean;
for(;;){
int i = Integer.parseInt(buffer.readLine());
if(i == -1){
break;
}else{
sum += i;
count += 1;
}
}
mean = sum/count;
System.out.println(mean);
}
need suggestion
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Ankush Rajput
25 September 2018, 11:07
for(::) ? What's this for ?
Specify conditions of loop.
0
Niranjan Soni
25 September 2018, 11:12
that is for getting the input from keyboard.
because inumber of inputs are not fixed.it may be infinite.
0
Ankush Rajput
25 September 2018, 11:36
You can do that as below -
String s;
while ((s=buffer.readLine()) !=null){
//read input
}
this will continue until there is no more input.
But you cannot use a loop without specifying any conditions.
0
Niranjan Soni
25 September 2018, 12:59
but my code is also working perfectly and it got approved.
i just wanted clarification if i coded in right way.
0
Ankush Rajput
25 September 2018, 13:40
It got accepted ? great. I thought for loop without any conditions is invalid syntax. Searched for it just now and it turns out for(;;) works as an infinite loop.
learnt something new today. Thank you :)
+2
hidden #10366080
1 December 2018, 12:30
is that a good coding practice though?
0