View Full Version : Program that asks the user to enter 5 numbers and then displays them at the end

12-04-2011, 09:11 AM
im writing a program that asks the user to enter 5 numbers and then when it has all 5 numbers it should display all the numbers at the end, but i cant make it work so please help me out.

import java.util.Scanner;

public class Lab
public static void main(String[] args)
Scanner keyboard = new Scanner(System.in);

for(int count = 0; count < 5; count++)
System.out.println("Enter a integer.");
int numbers = keyboard.nextInt();

12-04-2011, 04:36 PM
This is easiest done as an array. The problem you will have with the above is that numbers will only contain the last input digit since its overwritten during each iteration of the loop. Create an array prior to the loop, and insert each iteration as a new item within the array. After the loop, you can then loop again and print each item within the array.

You should do some error checking as well on the input, but I'm guessing that this is a school assignment, in which case exceptions may not yet have been covered.