Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
12-04-2011, 09:11 AM #1
- Join Date
- Oct 2011
- 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.
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 #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- 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.