Math Example - Can you find the bug?

/*In this program, we want the user to enter 10 numbers. Then, we will calculate and print out the average, and the lowest of the numbers. But there's a problem...can you spot it? The solution is at: https://codepad.co/snippet/Ex7zEYNE A detailed explanation is available at: https://medium.com/datadriveninvestor/debugging-pitfall-getting-scanner-input-in-java-41e42874cbe. */ import java.util.Scanner; public class MathExample { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.println("Please enter 10 numbers, each separated by a space, then press Enter"); int i = 0; double sum = 0; double lowest = 0; while (reader.hasNextDouble()) { if (i < 10) { sum += reader.nextDouble(); if(i == 0) { lowest = reader.nextDouble(); } else { if(next < lowest) { lowest = reader.nextDouble(); } } i++; if (i == 10) { break; } } } System.out.println("The mean is " + sum / 10); System.out.println("The lowest number is : " + lowest); } }
In this program, we want the user to enter 10 numbers. Then, we will calculate and print out the average, and the lowest of the numbers.
But there's a problem...can you spot it?

The solution is at: https://codepad.co/snippet/Ex7zEYNE
A detailed explanation is available at: https://medium.com/datadriveninvestor/debugging-pitfall-getting-scanner-input-in-java-41e42874cbe.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.