Brandon - 1 year ago 62
Java Question

# Java - Array Output: Not counting correctly?

I'm writing a program that asks the user to input temperatures for 10 days and then it takes the average and displays it and then outputs which days were above average in temperature.

BUT, for some reason my day counter is wrong, I want it to output day 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ...

Also if that is fixed then the average output days should be fixed too.

``````    public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
System.out.println("Welcome to the above average temperature tester program.");

int [] numDays = new int [10];
int sum = 0;

for (int i = 0; i < numDays.length; i++) {
System.out.println("Please enter the temperature for day " +i + 1);
numDays[i] = userInput.nextInt();
sum += numDays[i];
}
double average = (double) sum / numDays.length;
System.out.println();
System.out.println("Average = " + average);

for (int i=0; i < numDays.length; i++)
{
if (numDays[i] > average)
{
System.out.println ("The days that were above average were:");
System.out.println ("day " +i+ " with " +numDays[i]);
}
}
}
``````

Code Output:

Please enter the temperature for day 01

97

Please enter the temperature for day 11

87

Please enter the temperature for day 21

85

Please enter the temperature for day 31

76

Please enter the temperature for day 41

83

Please enter the temperature for day 51

104

Please enter the temperature for day 61

78

Please enter the temperature for day 71

76

Please enter the temperature for day 81

90

Please enter the temperature for day 91

85

Average = 86.1

The days that were above average were:

day 0 with 97

The days that were above average were:

day 1 with 87

The days that were above average were:

day 5 with 104

The days that were above average were:

day 8 with 90

Change

``````System.out.println("Please enter the temperature for day " +i + 1);
``````

to

``````System.out.println("Please enter the temperature for day " + (i + 1));
``````

or

``````System.out.printf("Please enter the temperature for day %d%n", i + 1);
``````

And, change

`````` System.out.println ("day " +i+ " with " +numDays[i]);
``````

to

``````  System.out.println("day " + (i + 1) + " with " + numDays[i]);
``````

or

``````  System.out.printf("day %d with %d%n", i + 1, numDays[i]);
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download