I need help with this problem, if someone can help me with this it would be appreciated.

Part 1: Write a program that uses a while loop to loop 5 times. Each time it should prompt the user to input an integer and should add it onto a sum. After all five numbers have been added, the program should display the total and the average. The average should be double, not an int. Print the source code along with the output you get when you enter the following five numbers. 6, 8, 3, 12, 9

Part 2: Modify the program from part 1 to use a for loop instead of a while loop. Print out the source code and enter the same five numbers.

Part 3: Modify the program from part 2 to use the for loop again, but to input the number of integers to enter. Print out the source code and enter 4 the following integers 8, 11, 2, 1.

Please Help Me If you can...Thanks Alot

Well no one will give you the code, i think you know that.

Try to figure it out 1st by yourself. Try coding some lines. I assume this is a homework so I assume that somehow your teacher taught you how to use the while and if loops.

Cmon show some effort.

Its a simple enough program........if u try it out im sure u can figure it out.....
all the best :)

Sounds like you're new at this. Have a go at writing it and see where you get.
First thing to do is to work out what the steps are.

So for part one, you've been told to use a while loop to prompt a user to enter five integers and then report out the total and the average. Before you start writing Java, write out the steps that you'll need to take. What will you need to keep track of? The best way to go about this, and this is always useful, is to walk through the steps with a pad of paper and a pencil.

I think you'll find this isn't so difficult.

package looping;

import java.util.Scanner;

 * @author owner
public class looping {

     * @param args the command line arguments
    public static void main(String[] args) {

        int counter = 1;
        int number;
        int sum = 0;
        double average;

        Scanner keyboard = new Scanner(System.in);

        while (counter <=5)
            System.out.print("Please input an integer: ");
            number = keyboard.nextInt ();
            sum = sum + number;

            System.out.println("Sum = " + sum );
            average = sum / 5.0;
            System.out.println("Average - " +average);

[B]Can someone tell me how can i change this while loop to a for loop and a do-while loop and still display 5 numbers[/B]

normally, a for loop has the following syntax


Of course, this can change quite dramatically, but this is the basic that you will use alot. Index starts at 0, index++ adds 1 to the index each time the loop runs. The loop will end once index reaches sumNumber

One thing you should know about a do..while loop. It is always executed at least once no matter if the condition is true or not, whereas a while loop will only execute once its condition is true.

counter = 7;
System.out.println("While loop");

System.out.println("Do while");

Even though they test the same condition, and counter is not <= 5. The do while will still be entered 1 time.

The only thing I'd add to Akill's posting is to point out that a for loop is a special sort of while loop, whose syntax is defined particularly for iteration. Any for loop can be rewritten as a while loop, like so:

for (initialize; condition; iterate)


initialize;  //optional
while (condition)

A do...while is essentially the same thing, except the test falls after the first time through the body of the loop.

For the jvm, these are all essentially the same code, the differences are entirely a convenience for the programmer.

Can a moderator or a staff member please help me?

please check your private messange..thanks!