# Finding sum, average, and maximum values of 5 integers using a loop

Page 1 of 1

## 3 Replies - 515 Views - Last Post: 08 March 2013 - 07:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314777&amp;s=91cd4a969c09e104684fcc22628bc9fc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JackB121

Reputation: -1
• Posts: 11
• Joined: 08-March 13

# Finding sum, average, and maximum values of 5 integers using a loop

Posted 08 March 2013 - 12:11 PM

Okay, so my homework reads as follows:

Write a Java program to do the following using a loop:
Prompt the user to input 5 integer numbers (one at a time).
Calculate the sum, average and maximum of all the five numbers and display them.

I have the syntax for the loop down (I'm using the for loop) but I can't figure out how to format my code to be able to add the 5 inputted integer values together. Do I have to make a separate variable for each integer value? Any help would be appreciated. I have some code I've done, it is not finished whatsoever so forgive me, just using it to show you the direction I am headed in.

```//This program is to calculate the sum, average, and maximum of 5 numbers.

import javax.swing.JOptionPane;

public class numberFacts
{
public static void main (String[] args)
{

String input;
int numbers = 5;
int sum = 0;
int average = 0;
int maximum = 0;
int number1, number2, number3, number4, number5;

for(int counter = 0; counter<=numbers; counter++)
{
input = JOptionPane.showInputDialog("Please enter an integer number.");
numbers = Integer.parseInt(input);

sum += counter;
}
JOptionPane.showMessageDialog(null,
"The sum of the numbers is " + sum);
System.exit(0);
}
}

```

Once I figure out how to do the sum, the average is a piece of cake... I'm just tripped up on this part.

I know it's rough. First programming class ever so cut me some slack />/>/>

Is This A Good Question/Topic? 0

## Replies To: Finding sum, average, and maximum values of 5 integers using a loop

### #2 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Finding sum, average, and maximum values of 5 integers using a loop

Posted 08 March 2013 - 12:15 PM

Instead of adding the counter to the sum, you want to add the user input to the sum. You stored the input in the numbers variable so use that.

But before you do you are using the numbers variable in the condition for the for loop. If you change the value you are going to get some unexpected behaviour when the loop runs more/less than 5 times.

Instead make a new int in the for loop. Say called num and assign it the Integer.parseInt() result. Then add that to the sum variable.

You also don't need the number1, number2 variables so just cut those out.

### #3 JackB121

Reputation: -1
• Posts: 11
• Joined: 08-March 13

## Re: Finding sum, average, and maximum values of 5 integers using a loop

Posted 08 March 2013 - 12:25 PM

Thanks so much, super helpful! And thanks for the ultra fast reply, I was worried as this assignment is due at 5 pm tonight... No more saving my homework for the last minute

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Finding sum, average, and maximum values of 5 integers using a loop

Posted 08 March 2013 - 07:27 PM

Duplicated topic