Results 1 to 2 of 2
Thread: Help with Array
- 07-13-2007, 09:53 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 44
- Rep Power
- 0
Help with Array
Hi, I have a question regarding arrays in Java.
I want a user to
- decide how many numbers he want to type in and store in the array.
- Let the user type in the numbers.
- Show the numbers from the array backwards.
This is how my code looks like now. What am I doing wrong? Can anyone point me in the right direction?
This is how it currently shows with that codeJava Code:import java.util.Scanner; public class steg4egenlabb2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int tal = 0; System.out.print("Enter the amount of numbers you want to store in the array : "); tal = keyboard.nextInt(); System.out.println("Enter " + tal + " numbers:"); int[] antal = new int[tal]; antal[0] = keyboard.nextInt(); System.out.println("The numbers written backwards:"); System.out.println(antal[0]); } }
ThanksJava Code:Enter the amount of numbers you want to store in the array : 5 Enter 5 numbers: 1 2 3 4 5 The numbers written backwards: 1
- 08-07-2007, 04:32 AM #2
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
Hello, I have taken the liberty to rewrite your class in order to help you out.
The output of running this program isJava Code:import java.util.Scanner; public class steg4egenlabb2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int tal = 0; System.out.print("Enter the amount of numbers you want to store in the array : "); tal = keyboard.nextInt(); int[] antal = new int[tal]; // loop array elements and get values from the user for( int i = 0; i < tal; i++ ) { System.out.print( "Enter Value for Element " + (i+1) + ": " ); antal[i] = keyboard.nextInt(); } System.out.println("The numbers written backwards:"); // loop through the array backwards and output the values. for( int i = antal.length - 1; i >= 0; i-- ) { System.out.println( "Element at position " + ( 1 + i ) + " is: " +antal[i]); } } }
Greetings.Java Code:Enter the amount of numbers you want to store in the array : 5 Enter Value for Element 1: 1 Enter Value for Element 2: 2 Enter Value for Element 3: 3 Enter Value for Element 4: 4 Enter Value for Element 5: 5 The numbers written backwards: Element at position 5 is: 5 Element at position 4 is: 4 Element at position 3 is: 3 Element at position 2 is: 2 Element at position 1 is: 1
Similar Threads
-
Array Help
By bluegreen7hi in forum New To JavaReplies: 2Last Post: 03-28-2008, 02:25 AM -
can anyone help... 2d Array
By Mark1989 in forum New To JavaReplies: 2Last Post: 03-12-2008, 08:59 PM -
Would appreciate your help with 2d Array..
By cloudkicker in forum New To JavaReplies: 1Last Post: 02-11-2008, 02:34 PM -
2D array
By bluekswing in forum New To JavaReplies: 2Last Post: 01-15-2008, 05:57 PM -
Bounded Array
By bugger in forum New To JavaReplies: 4Last Post: 01-04-2008, 09:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks