import java.util.Scanner;
public class Simple
{
public static void main(String[] args)
{

Scanner scanner = new Scanner(System.in);

System.out.println("How many number?");
int numNumber = scanner.nextInt();

int sum = 0;
int []numbers=new int[numNumber];
int highest=numbers[0];
int lowest=numbers[0];
for(int i = 0;i< numbers.length ; i++)
{

	System.out.println("Please input number" + i + ":");
	 numbers[i]= scanner.nextInt();

	sum += numbers[i];
	for(int j=0; j<numbers.length;j++)
	{
		if(numbers[j]>highest)
		{
			highest=numbers[j];
		}

		else if(lowest>numbers[j])

		{
			lowest=numbers[j];
		}
	}



}
System.out.println("Sum of the number is" + sum);

double average = (double)sum/numNumber;
System.out.println(" The average is" + average);


System.out.println("The largest number is" + highest);
System.out.println("The smallest number is" + lowest);
}
}

why the lowest number is zero?..
is there anything wrong with the array code?..

Recommended Answers

All 6 Replies

You initialized the variable 'lowest' with the value of numbers[0]. Note that numbers[0] contains 0 at that point in time. See your problem?

You initialize the array, but you don't put values in it. So by default the values that it has are zero because the int variables are initialized to zero.

This might not seem serious but if the array was String it would null. So next time, you don't just create the array, you put values in it as well

This might not seem serious but if the array was String it would null. So next time, you don't just create the array, you put values in it as well

Um, no, the problem here was not how the array was initialized with default values.

Both Rashakil Fol's posts are correct.

Initialise your high/low variables like this

int highest = Integer.MIN_VALUE;
int lowest = Integer.MAX_VALUE;

and you can't go wrong. ;-)

Um, no, the problem here was not how the array was initialized with default values.

You are right. I have missed the part of the code where the poster asks the user for input.
Usually I initialize the lowest, highest values with the first value of the array, like the poster. But in this case the poster did so before putting any values, which was the problem as you mentioned

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.