Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Program that asks the user to enter 5 numbers and then displays them at the end

    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();
    }
    }
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •