Hi all,
how can I count numbers thats 5.5 or lower?Code:double numbers[] = {9.8, 5.3, 6.2, 5.5, 5.4, 7.7, 8.1, 3.7, 1.8, 4.6};
in the example above it must be 5 numbers under 5.5
Thanks
Printable View
Hi all,
how can I count numbers thats 5.5 or lower?Code:double numbers[] = {9.8, 5.3, 6.2, 5.5, 5.4, 7.7, 8.1, 3.7, 1.8, 4.6};
in the example above it must be 5 numbers under 5.5
Thanks
You need two constructs for this to work. First you have to go through all of the numbers in the array -- what code structure will allow you to loop through each item in the array?
Beware when using floating point numbers in equality statements.
You would expect the output of the above code to be 3 but is in fact only 2.Code:class DoubleTrouble {
public static void main(String[] args) {
int count = 0;
double[] values = {1, 7.0, 5.4, 5.5, 5.6, 5.499999999999999999999999999999};
for(double d : values) {
if(d < 5.5) {
count++;
}
}
System.out.println(count);
}
}
Can anyone tell me the difference between:
,Code:double[] numbers = new double[]{1.0, 1.5, 2.0, 2.5, 3.0, 3.5};
andCode:double[] numbers = {1.0, 1.5, 2.0, 2.5, 3.0, 3.5};
I've tried all those 3 codes, and they work the same for me...Code:double numbers[] = {1.0, 1.5, 2.0, 2.5, 3.0, 3.5};
Are these codes just the same?
yes all 3 are ways to do the same thing, although the first one is redundant. I personally preferQuote:
double[] numbers = {values};