/*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.
This example is the fix from the buggy example at https://codepad.co/snippet/bAGpUdAb.
We've added an extra variable to store the next number in the list.
*/
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())
{
double next = reader.nextDouble();
if (i < 10)
{
sum += next;
if(i == 0)
{
lowest = next;
}
else
{
if(next < lowest)
{
lowest = next;
}
}
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.
This example is the fix from the buggy example at https://codepad.co/snippet/bAGpUdAb.
We've added an extra variable to store the next number in the list. A detailed explanation is available at https://medium.com/datadriveninvestor/debugging-pitfall-getting-scanner-input-in-java-41e42874cbe.
This example is the fix from the buggy example at https://codepad.co/snippet/bAGpUdAb.
We've added an extra variable to store the next number in the list. 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.