3 Replies - 529 Views - Last Post: 08 March 2013 - 07:27 PM Rate Topic: -----

#1 JackB121   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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 :helpsmilie:/>/>/>

Is This A Good Question/Topic? 0
  • +

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

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 JackB121   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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 :blink:
Was This Post Helpful? -1
  • +
  • -

#4 pbl   User is offline

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

Reputation: 8378
  • View blog
  • 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
Question already answered... you are wasting our time

Topic closed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1